2016-12-27 23 views
1

これは学校のプロジェクトで、パスワードでメッセージを暗号化する必要があります。だから、私はサーバーに接続し、それはすべての権利が正しいと思われるが、cipher.init(Cipher.ENCRYPT_MODE, secret);それはすべてのコードを壊したので、私はsoutの前に誰かが私を助けることができないのですか?パスワードでメッセージを暗号化するjava

try { 
     String sName = "localhost"; 
     int port = 8080; 

     Socket client = new Socket(sName, port); 
     OutputStream os = client.getOutputStream(); 
     OutputStreamWriter osw = new OutputStreamWriter(os); 
     BufferedWriter bw = new BufferedWriter(osw); 

     //String password = null; 
     String password = fieldPassword.getText(); 
     char[] a = password.toCharArray(); 
     byte[] salt = new byte[256]; 

     SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
     KeySpec spec = new PBEKeySpec(a, salt, 65536, 256); 
     SecretKey tmp = factory.generateSecret(spec); 
     SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); 


     Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     cipher.init(Cipher.ENCRYPT_MODE, secret); 
     String msg = jTextField2.getText(); 
     byte[] text = msg.getBytes(); 
     byte[] ciphertext = cipher.doFinal(text); 

     String hex=DatatypeConverter.printHexBinary(ciphertext); 
     System.out.println(hex); 

     String sendMessage = "{'command':'send','dst':'" + jTextField1.getText() + "','msgencrypt':'" + hex +"'}";`this is json to send to the server` 
     bw.write(sendMessage); 
     bw.flush(); 
} catch (IOException ex) { 
     Logger.getLogger(NewJFrame3.class.getName()).log(Level.SEVERE, null, ex); 
}catch (Exception e){ 
} 
+0

があなたのコード例で発生した例外を指定してください。 'catch(Exception e)'句にロガー文を追加してください。 – swiedsw

+0

サイドノート:塩はランダムなデータであると考えられています。あなたの場合、それはゼロの配列です。それはランダムではありません。 –

+0

空のキャッチブロックがあります。それは非常に悪い考えです。完全なスタックトレースを印刷または記録する必要があります。もしあなたがそうしなければ、あなたはそれを知らずに悪いことが起こります。 – duffymo

答えて

0

あなたがして256ビットのキーサイズのAESキーを要求されています

KeySpec spec = new PBEKeySpec(a, salt, 65536, 256); 

しかしパッチ未適用の JVMがimport/export regulationsによるこの大きなAESキーを許可しません。

Eigtherは無制限強度の管轄ポリシーファイルを使用してJVMにパッチを適用または使用して128ビットキーで試してみてください。

KeySpec spec = new PBEKeySpec(a, salt, 65536, 128); 
関連する問題