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){
}
があなたのコード例で発生した例外を指定してください。 'catch(Exception e)'句にロガー文を追加してください。 – swiedsw
サイドノート:塩はランダムなデータであると考えられています。あなたの場合、それはゼロの配列です。それはランダムではありません。 –
空のキャッチブロックがあります。それは非常に悪い考えです。完全なスタックトレースを印刷または記録する必要があります。もしあなたがそうしなければ、あなたはそれを知らずに悪いことが起こります。 – duffymo