2015-05-15 16 views
0

我有一个Java项目通过无线网络对图像进行身份验证。我正在使用河豚加密图像。我面临的问题是如何将接收器的对称密钥发送给接收器,以便他可以解密图像。我对密码学相对来说比较陌生。请包括代码片段来说明相同的情况。 如何通过java中的网络发送河豚键

package ClientModule; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.swing.*; public class ImageEncryption_Client { KeyGenerator keyGenerator = null; public static SecretKey secretKey = null; public static Cipher cipher = null; ImageEncryption_Client(){ try { /** * Create a Blowfish key */ keyGenerator = KeyGenerator.getInstance("Blowfish"); secretKey = keyGenerator.generateKey(); System.out.println(secretKey.toString()); /** * Create an instance of cipher mentioning the name of algorithm * - Blowfish */ cipher = Cipher.getInstance("Blowfish"); System.out.println(cipher.toString()); } catch (NoSuchPaddingException ex) { System.out.println(ex); } catch (NoSuchAlgorithmException ex) { System.out.println(ex); } } private void encrypt(String srcPath, String destPath) { File rawFile = new File(srcPath); File encryptedFile = new File(destPath); InputStream inStream = null; OutputStream outStream = null; try { /** * Initialize the cipher for encryption */ cipher.init(Cipher.ENCRYPT_MODE, secretKey); /** * Initialize input and output streams */ inStream = new FileInputStream(rawFile); outStream = new FileOutputStream(encryptedFile); byte[] buffer = new byte[1024]; int len; while ((len = inStream.read(buffer)) > 0) { outStream.write(cipher.update(buffer, 0, len)); outStream.flush(); } outStream.write(cipher.doFinal()); inStream.close(); outStream.close(); } catch (IllegalBlockSizeException ex) { JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE); System.out.println(ex); } catch (BadPaddingException ex) { JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE); System.out.println(ex); } catch (InvalidKeyException ex) { JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE); System.out.println(ex); } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE); System.out.println(ex); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE); System.out.println(ex); } } void enc(String filename)//, String dir) { String fileToEncrypt = filename; String arr[]=filename.split("\\."); String encryptedFile = arr[0]+"_encrypted."+arr[1]; String directoryPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\"; encrypt(directoryPath + fileToEncrypt, directoryPath + encryptedFile); } public static void main(String... kkk) { new ImageEncryption_Client().enc("Koala.jpg");//,""); } }

回答

0

首先,我不会在没有做更多研究或咨询安全专家的情况下进入这个领域。在加密

一个真正的好资源专门针对开发者是:https://www.schneier.com/books/cryptography_engineering/

现在到回答你的问题。我首先会质疑你是否真的需要进行密钥交换,或者你是否可以放弃在双方建立预共享的密钥/密钥对。

密钥交换

密钥交换之类的问题,你问一下。有许多实现: http://en.wikipedia.org/wiki/Key_exchange

的Diffie Hellman的是一个典型的做法是在SSL流行和TLS: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

的Diffie Hellman的一个Java实现的例子可以在这里找到: http://docs.oracle.com/javase/7/docs/technotes/guides/security/crypto/CryptoSpec.html#DH2Ex

预共享密钥

密钥交换非常复杂,需要您拥有通信的双方。另一种方法是在通信的两端放置一个共享的秘密。最简单的方法是共享对称密钥。 http://en.wikipedia.org/wiki/Shared_secret

稍微更安全的方法是使用不对称密钥(公钥密码),其中每一方生成它自己的公用私钥对,然后与另一端预先共享公钥。因此,在Alice和Bob想要安全通信的典型示例中,Alice将给Bob她的公钥,并且Bob将用她的公钥加密发送给Alice的所有数据。她可以用她的私钥解密它。这种方法的优点是,如果任何一方受到攻击,攻击者无法读取为另一方加密的消息。更多细节在这里列出: http://en.wikipedia.org/wiki/Public-key_cryptography

构建最后一个方法的第三个和稍微更细化的方法是为每个图像生成一个对称密钥并使用该密钥加密图像。然后使用您要发送数据的一方的公钥对对称密钥进行加密。当通过电线发送图像时,您将包含加密密钥和加密图像。这种模式非常有用,因为对称加密速度更快,更适合像图像这样的大文件,但以这种方式组合它们,您仍然可以获得使用公钥密码保护传输的好处。

如果您恰好在AWS上完成这项工作,他们的KMS服务将使跨服务器拥有共享加密密钥变得非常简单。他们也使用类似于上一个模型的多级加密方法,但是在这种情况下,主密钥存储在硬件安全模块(HSM)上,这具有额外的好处,即万能密钥永远不会被知道,并且永远不会被取消它所在的芯片: http://aws.amazon.com/kms/