Java 到 JS 和 JS 到 Java 使用 cryptojs 加密
Java to JS and JS to Java encryption using cryptojs
几周前我开始使用这个 post 并且工作得很好:
Compatible AES algorithm for Java and Javascript
现在,我需要进行反向操作,但是在 java 中,我得到了这个异常:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
这是我在Java脚本中做的"reverse"操作:
var rkEncryptionKey = CryptoJS.enc.Base64.parse('u/Gu5posvwDsXUnV5Zaq4g==');
var rkEncryptionIv = CryptoJS.enc.Base64.parse('5D9r9ZVzEYYgha93/aUK2w==');
function encryptString(stringToEncrypt) {
var utf8Stringified = CryptoJS.enc.Utf8.parse(stringToEncrypt);
var encrypted = CryptoJS.AES.encrypt(utf8Stringified.toString(), rkEncryptionKey, {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: rkEncryptionIv});
return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
}
我以为就这些了?
[编辑]
加密后的字符串如下:{"company_name":"asdfasdfasd","customer_name":"asdfasdfasdfasdf","phone_number":"asdfasdfasdfasdf","email":"asdfasdfasdfasdfads"}
从 java 到 java 进行加密/解密时有效,从 java 到 java 脚本进行加密/解密时,也有效,但从 java脚本到 java,不工作。
Java代码
public String toJson(final String encrypted) {
try {
SecretKey key = new SecretKeySpec(Base64.decodeBase64("u/Gu5posvwDsXUnV5Zaq4g=="), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(Base64.decodeBase64("5D9r9ZVzEYYgha93/aUK2w=="));
byte[] decodeBase64 = Base64.decodeBase64(encrypted);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return new String(cipher.doFinal(decodeBase64), "UTF-8");
} catch (Exception e) {
throw new RuntimeException("This should not happen in production.", e);
}
}
改变
return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
到
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
encrypted
对象通常字符串化为 OpenSSL 格式,其中可能也包含盐。如果您只对实际的密文感兴趣,那么您需要将 ciphertext
属性.
字符串化
请注意,toString()
采用可选的编码函数。使用您需要的那个。
几周前我开始使用这个 post 并且工作得很好: Compatible AES algorithm for Java and Javascript
现在,我需要进行反向操作,但是在 java 中,我得到了这个异常:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
这是我在Java脚本中做的"reverse"操作:
var rkEncryptionKey = CryptoJS.enc.Base64.parse('u/Gu5posvwDsXUnV5Zaq4g==');
var rkEncryptionIv = CryptoJS.enc.Base64.parse('5D9r9ZVzEYYgha93/aUK2w==');
function encryptString(stringToEncrypt) {
var utf8Stringified = CryptoJS.enc.Utf8.parse(stringToEncrypt);
var encrypted = CryptoJS.AES.encrypt(utf8Stringified.toString(), rkEncryptionKey, {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: rkEncryptionIv});
return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
}
我以为就这些了?
[编辑]
加密后的字符串如下:{"company_name":"asdfasdfasd","customer_name":"asdfasdfasdfasdf","phone_number":"asdfasdfasdfasdf","email":"asdfasdfasdfasdfads"}
从 java 到 java 进行加密/解密时有效,从 java 到 java 脚本进行加密/解密时,也有效,但从 java脚本到 java,不工作。
Java代码
public String toJson(final String encrypted) {
try {
SecretKey key = new SecretKeySpec(Base64.decodeBase64("u/Gu5posvwDsXUnV5Zaq4g=="), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(Base64.decodeBase64("5D9r9ZVzEYYgha93/aUK2w=="));
byte[] decodeBase64 = Base64.decodeBase64(encrypted);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return new String(cipher.doFinal(decodeBase64), "UTF-8");
} catch (Exception e) {
throw new RuntimeException("This should not happen in production.", e);
}
}
改变
return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
到
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
encrypted
对象通常字符串化为 OpenSSL 格式,其中可能也包含盐。如果您只对实际的密文感兴趣,那么您需要将 ciphertext
属性.
请注意,toString()
采用可选的编码函数。使用您需要的那个。