2015-05-15 44 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/