只要私钥保持秘密我要为安全保密,解密,当我在我的应用程序出现以下错误:javax.crypto中.BadPaddingException:鉴于最终块未正确填充的Java加密通过客户端和解密的服务器,使用PBKDF2WithHmacSHA1和AES/CBC/PKCS5Padding
代码:
// Encryption, client side
byte[] plainData = "hello plaintext!".getBytes("UTF-8");
byte[] salt = new byte[64];
new SecureRandom().nextBytes(salt);
KeySpec spec = new javax.crypto.spec.PBEKeySpec("password".toCharArray(), salt, 1024, 256);
SecretKey sk = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sk.getEncoded(), "AES"));
byte[] iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
byte[] ciphertext = cipher.doFinal(plainData);
System.out.println("ciphertext: "+new String(ciphertext, "UTF-8")); // cipher
// Decryption, server side
KeySpec spec2 = new javax.crypto.spec.PBEKeySpec("password".toCharArray(), salt, 1024, 256);
SecretKey sk2 = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec2);
Cipher cipher2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher2.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sk2.getEncoded(), "AES"), new IvParameterSpec(iv)); // Get the same IV value from client/encryptor aswell, still random
String plaintext = new String(cipher2.doFinal(ciphertext), "UTF-8");
System.out.println("decrypted plaintext: "+plaintext); // plain
是它的盐是导致问题的随机性?
我可以罚款解密它,当我使用在客户端的对象引用的,但我需要在服务器上我自己的情况。
十分感谢提前纠正我的错误(S)!
* 编辑:*代码更新和纠正
的衬垫例外指向该代码顺便说一句:字符串明文=新字符串(cipher2.doFinal(密文), “UTF-8”); – gorn
你有没有使用SSL的原因? – NullUserException