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

前端 未结 3 1365
一个人的身影
一个人的身影 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:10

    Keep the following things in mind while encrypting and decrypting strings,

    ENCRYPTION:

    1. Convert string to byteArray using toByteArray(Charsets.UTF-8) and always specify the charset with UTF-8.

    2. Encrypyt the above byteArray using cipher.doFinal(byteArray).

    3. Now this is not enough, you need to do base64 encoding for that encrypted byteArray using Base64.encode(encryptedByteArray, Base64.DEFAULT)

    4. Remember this again returns byteArray , if you want to convert to string, use toString(Charsets.UTF-8) and most importantly specify the charset again as UTF-8 and then process or store it in DB as you wish.

    DECRYPTION:

    1.Get the encrypted string and first step while decrypting is to decode the encrypted string using base64.decode(encryptedString.toByteArray(Charsets.UTF-8), Base64.DEFAULT)

    1. Now decrypt the decoded byteArray by using cipher.dofinal(decodedByteArray).

    2. Convert the Decrypted byteArray to String using toString(Charsets.UTF-8). NOTE:Always specify the charset. This returns the original string.

    I know I have not shared any code but trust me the flow is the important part while encrypting and decrypting a string..

提交回复
热议问题