大家好,欢迎来到IT知识分享网。
几个星期前我上了这篇文章并完美地工作:
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)
这是我在JavaScript中执行的“反向”操作:
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到javascript这样做时,也可以工作,但是从javascript到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);
}
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24342.html