javax.crypto.IllegalBlockSizeException: last block incomplete in decryption - Decrypting an encrypted AES String

前端 未结 3 1391
一个人的身影
一个人的身影 2020-12-29 08:42

I am trying to decrypt the string \"~9?8?m???=?T?G\" that I receive from a back-end server which uses OpenSSL to encrypt the String using AES-256-CBC. There is

3条回答
  •  粉色の甜心
    2020-12-29 09:18

    You should decode the string instead of encoding the platform specific representation of the string, right at the start of your method.

    byte[] base64TextToDecrypt = Base64.decodeBase64(textToDecrypt);
    

    or more precisely:

    byte[] bytesToDecrypt = Base64(base64TextToDecrypt);
    

    if you name your variables correctly.

    In general, each time you (feel like you have to) use the String.getBytes(): byte[] method or the String(byte[]) constructor you are likely doing something wrong. You should first think about what you are trying to do, and specify a character-encoding if you do need to use it.

    In your case, the output in the converted variable is probably character-encoded. So you you could use the following fragment:

    String plainText = new String(converted, StandardCharsets.UTF_8);
    System.out.println(plainText);
    

    instead of what you have now.

提交回复
热议问题