问题描述
几周前我发表了这篇文章,并且工作完美:现在,我需要做相反的操作,但是一旦在java中,我得到这个异常:
javax.crypto.BadPaddingException:给定最终块未正确填充
在com.sun.crypto.provider.CipherCore .doFinal(CipherCore.java:966)
在com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
在com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher .java:436)
在javax.crypto.Cipher.doFinal(Cipher.java:2165)
这是我在JavaScript中执行的反向操作:
var rkEncryptionKey = CryptoJS.enc.Base64.parse 'U / Gu5posvwDsXUnV5Zaq4g ==');
var rkEncryptionIv = CryptoJS.enc.Base64.parse('5D9r9ZVzEYYgha93 / aUK2w ==');
函数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.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(这不应该在生产中发生),e);
}
}
更改
返回CryptoJS.enc.Base64.parse(encrypted.toString())。toString();
至
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
加密的对象通常会引用到OpenSSL格式也可能含有盐。如果您只对实际的密文感兴趣,则需要对
密文
属性进行字符串化。
请注意 toString()
采用可选的编码功能。使用您需要的。
I got on this post a couple of weeks ago and worked perfectly:Compatible AES algorithm for Java and Javascript
Now, I need to do the reverse operation, but once in java, I am getting this exception:
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)
This is my "reverse" operation I did in 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();
}
I though this would be all?
[EDIT]
Encrypted string is the following: {"company_name":"asdfasdfasd","customer_name":"asdfasdfasdfasdf","phone_number":"asdfasdfasdfasdf","email":"asdfasdfasdfasdfads"}
When doing the encrypt / decrypt from java to java it works, when doing so from java to javascript, also works, but from javascript to java, not working.
Java Code
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);
}
}
Change
return CryptoJS.enc.Base64.parse(encrypted.toString()).toString();
to
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
The encrypted
object usually stringifies into an OpenSSL format which might also contain the salt. If you're only interested in the actual ciphertext, then you need to stringify the ciphertext
property.
Note that toString()
takes an optional encoding function. Use the one that you need.
这篇关于Java到JS和JS到使用cryptojs的Java加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!