0

I am trying to have an encryption/decryption setup. The encryption mode is ECB using the AES algorithm. For encryption, I am using CryptoJS as part of my application. The following code is used to encrypt:

 var keyHex = CryptoJS.enc.Utf8.parse(key); alert(keyHex + ":" + keyHex.toString().length); var cipherText = CryptoJS.enc.Base64.stringify(CryptoJS.AES.encrypt(inputString, keyHex, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding}).ciphertext); console.log("Encrypted: " + cipherText); 

For decryption, I am using the AESCipherService of Shiro to decrypt. I have the following code:

public static String decryptUsingShiro(String base64EncodedData, String key) { byte[] base64decoded = Base64.decodeBase64(base64EncodedData.getBytes()); AesCipherService decryptService = new AesCipherService(); decryptService.setMode(OperationMode.ECB); decryptService.setPaddingScheme(PaddingScheme.PKCS5); ByteSource decrypt = decryptService.decrypt(base64decoded, key.getBytes()); return new String(decrypt.getBytes()); } 

I get the following exception:

Caused by: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher

Anything wrong I am doing on the encryption side?

1 Answer 1

1

You are using the padding mode "NoPadding", so the input must reach a complete block boundary (n * 16 bytes) in order to encrypt successfully. You should use PKCS7Padding instead. (note: PKCS #7 is referred to (incorrectly) as PKCS #5 in Java -- they are essentially equivalent and for these purposes, PaddingScheme.PKCS5 is correct.)

In addition, you should not use ECB mode. ECB is trivially broken and is not safe at all. Use an authenticated encryption mode (GCM/EAX/CCM) if possible, or CBC/CTR + HMAC if not.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. that worked like a charm. I will be changing my code to use the CBC mode instead of ECB.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.