本文介绍了Aes javascript加密 - java解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript加密邮件(使用crypto-js库)并在java中解密。

I'm trying to encrypt a message in javascript (using crypto-js library) and to decrypt it in java.

这是javascript代码:

This is the javascript code:

var key = CryptoJS.enc.Utf8.parse(aesPassword);
var ive  = CryptoJS.enc.Utf8.parse(aesIv);
var encryptedData = CryptoJS.AES.encrypt(dataToEncrypt, key, {mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: ive});

这是java代码:

final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        final SecretKeySpec key = new SecretKeySpec(aesPassword().getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(aesIv().getBytes("UTF-8")));
        byte[] decrypted = cipher.doFinal(DatatypeConverter.parseBase64Binary(message));

但是当我尝试用Java解密时,抛出了这个异常:
javax.crypto。 BadPaddingException:给定最终块未正确填充

But when I try to decrypt in Java this exception is thrown:javax.crypto.BadPaddingException: Given final block not properly padded

密码:6h2faBePVxpgyFSN
iv:NKOzRKrmEMKs1kE4
要加密的数据:{token:cMGOIrYlJm9lPhPW}

password: 6h2faBePVxpgyFSNiv: NKOzRKrmEMKs1kE4data to encrypt: "{token: cMGOIrYlJm9lPhPW}"

任何帮助?

提前致谢

推荐答案

我可能错了,但我认为BadPaddingException在这种情况下意味着你​​没有正确的密钥来成功执行解密。异常本质上意味着密钥太短或太多很久(我想)。

I may be wrong, but I think BadPaddingException in this case means that you don't possess the correct key to successfully perform the decryption.The exception essentially means that the key is either too short or too long (I think).

Try{
    String decrypted = aes.decrypt(...);
    System.out.println(decryted);
}catch(Exception e){

}

东西像上面的代码可能工作,因为只有在未捕获BadPaddingException时才会到达System.out,这可以在尝试可能的密钥进行解密时在循环中使用,例如,如果您尝试计算所有可能的密钥解密。

Something like the code above may work, as System.out is only reached when the BadPaddingException isn't caught, this could be used in a loop when trying possible keys for decryption, for example, if you were trying to calculate all possible keys for the decryption.

这篇关于Aes javascript加密 - java解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:18