Illegalblocksizeexception When Trying to Encrypt and Decrypt a String with Aes

IllegalBlockSizeException when trying to encrypt and decrypt a string with AES

When you encrypt a string with AES, you get an array of bytes back. Trying to convert those bytes directly to a string (new String(cipher.doFinal(plaintextBytes))) will cause all sorts of problems. If you require the output from your encryption method to be a string, then use Base64 rather than attempting a direct conversion. In your decryption method, convert the Base64 string back into a byte array before decrypting the byte array.

Also, do not use getBytes() since the output depends on the system defaults. Use getBytes("utf-8") or whatever. That eliminates ambiguity.

IllegalBlockSizeException when decrypting an encrypted JSON-String after reading from a file

Looks like you are writing out a binary file, but reading it in as a text.

How about reading the file with code similar to:

fun readEncrypted(file: String): ByteArray =
FileInputStream(file).use { it.readAllBytes() }

Once you decrypt the ByteArray you read from the file, you can convert the resulting ByteArray to a String.

IllegalBlockSizeException trying to decrypt string

A string that looks like fb/8asoHS/ShyCDV46t/Aw== is a Base64 encoded representation of the cipher text byte array.

The key started out 16 bytes long. Base64 encoding increases the length to 4/3 because it is using fewer characters to represent the bytes. 16 * 4/3 = 22. But Base64 needs to convert 3 bytes at a time, so it pads the bytes to be a multiple of 3, so 16 -> 18 * 4/3 = 24. The equals signs at the end are a typical artifact of this padding in Base64.

You are Base64 encoding the cipher text after you encrypt. You need to Base64 decode the cipher text before you decrypt.

Probably something like:

public String decrypt(String strCipherText) throws InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
String strDecryptedText = new String();

// Initialize the Cipher for Encryption
this.cipher.init(Cipher.DECRYPT_MODE, this.keyObj, this.ivObj);

// Decode the Base64 text
byte[] cipherBytes = Base64.decode(strCipherText);

// Decrypt the Data
// a. Initialize a new instance of Cipher for Decryption (normally don't reuse the same object)
// Be sure to obtain the same IV bytes for CBC mode.
// b. Decrypt the cipher bytes using doFinal method
byte[] byteDecryptedText = this.cipher.doFinal(cipherBytes);
strDecryptedText = new String(byteDecryptedText);

return strDecryptedText;
}

Cipher: What is the reason for IllegalBlockSizeException?

During decryption, one can only get an IllegalBlockSizeException if the input data is not a multiple of the block-size (16 bytes for AES).

If the key or the data was invalid (but correct in length), you would get a BadPaddingException because the PKCS #5 padding would be wrong in the plaintext. Very occasionally the padding would appear correct by chance and you would have no exception at all.


N.B. I would recommend you always specify the padding and mode. If you don't, you are liable to be surprised if the provider changes the defaults. AFAIK, the Sun provider converts "AES" to "AES/ECB/PKCS5Padding".



Related Topics



Leave a reply



Submit