JAVA中AES解密时,使用填充密码进行解密时,输入长度必须是16的倍数。

huangapple go评论84阅读模式
英文:

JAVA - AES Decryption - Input length must be multiple of 16 when decrypting with padded cipher

问题

我正试图解密从API获得的ResponseText变量。我得到了以下错误。

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be 
  multiple of 16 when decrypting with padded cipher

以下是我的解密代码片段。Decrytpt方法抛出了这个错误。

public static String decrypt(String encryptedText) throws Exception 
{
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, key, ivspec);
        byte[] encVal = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedValue = new String(encVal, "UTF-8");
        return decryptedValue;
}

由于加密值的长度过长,我没有在这里发布实际的加密值。我是Java新手。提前感谢您的帮助。

英文:

I am trying to decrypt the ResponseText variable which i get from an API. I am getting the following error.

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be 
  multiple of 16 when decrypting with padded cipher

Below is my code snippet for decrypting the response. The Decrytpt method is throwing the error.

public static String decrypt(String encryptedText) throws Exception 
{
        Key key = generateKey();
        Cipher chiper = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        chiper.init(Cipher.DECRYPT_MODE, key, ivspec);
        byte[] encVal = chiper.doFinal(encryptedText.getBytes("UTF-8"));
        Base64.Encoder base64Encoder = Base64.getEncoder();
        String decryptedValue = base64Encoder.encodeToString(encVal);
        String decryptedString= new String("");                
        return decryptedString;
}

I have not posted the actual encrypted value here as the length of the encrypted value is too high. I am new to Java. Thanks in advance.

答案1

得分: 2

你应该首先对密文进行Base64解码,然后解密二进制密文,然后对得到的明文进行UTF-8解码。

你并没有正确地反转加密流程(UTF-8编码,加密,Base64编码),换句话说。


解密中有一个generateKey()函数;除非它返回一个静态值(并且不生成一个值,正如方法名所暗示的那样),否则解密很可能会失败。所以要么方法名错误,要么解密方法错误。

似乎密文中也没有包含IV,这将意味着这是接下来需要处理的问题。

最后,你会想要了解如何处理异常以用于加密/解密流程。

英文:

You should probably base 64 decode the ciphertext, decrypt the binary ciphertext and then decode the resulting plaintext to UTF-8.

You haven't correctly reversed the encryption routine (encode UTF-8, encrypt, encode base64), in other words.


There is a generateKey() for the decryption; unless it returns a static value (and doesn't generate one, as the method name implies) decryption will likely fail. So either the name is wrong, or the decryption.

The IV doesn't seem to be included with the ciphertext either, which will mean that that's the next problem to deal with.

Finally, you will want to know how to handle exceptions for encryption / decryption routines.

huangapple
  • 本文由 发表于 2020年8月25日 21:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定