[英]encrypt a message with an password java
This is a school project, that i have to encrypt a message with a password. 这是一个学校项目,我必须使用密码加密消息。 So i make a connection to the server, it seem's everything right but in the line cipher.init(Cipher.ENCRYPT_MODE, secret);
所以我建立了与服务器的连接,看起来一切正常,但在行cipher.init(Cipher.ENCRYPT_MODE, secret);
it broke all the code, so i can't make a sout
before that, can someone help me? 它打破了所有的代码,所以我不能在那之前做出一个sout
,有人可以帮助我吗?
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){
}
You are requesting an AES key with key size of 256 bits with: 您正在请求密钥大小为256位的AES密钥:
KeySpec spec = new PBEKeySpec(a, salt, 65536, 256);
But an unpatched JVM will not allow this big an AES keys due to import/export regulations . 但是由于导入/导出规定 , 未修补的 JVM将不允许使用这么大的AES密钥。
Eigther patch your JVM with the Unlimited Strength Jurisdiction Policy files or try with a 128 bit key using: Eigther使用Unlimited Strength Jurisdiction Policy文件修补您的JVM,或使用以下方法尝试使用128位密钥:
KeySpec spec = new PBEKeySpec(a, salt, 65536, 128);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.