Decrypt AES-256 Cipher

912 views Asked by At

I am using Cipher.decrypt to decrypt AES-256. In saltOrIV I am sending it as follows:

var ivBytes = Encoding.fromHex("000000000000000000000000");
var ivSend = Encoding.toBase64(ivBytes);

cipher.decrypt(bodyBase64, key, "AES/GCM/PKCS5Padding", ivSend, 0);

But I am getting the following exception: com.demandware.beehive.core.internal.crypt.EncryptionException: com.demandware.beehive.core.internal.crypt.EncryptionException: java.security.InvalidAlgorithmParameterException: Unsupported parameter: javax.crypto.spec.IvParameterSpec

Can someone help me with what i am doing wrong?

Thanks

1

There are 1 answers

6
Maarten Bodewes On

First of all, "AES/GCM/PKCS5Padding" makes no sense, use "AES/GCM/NoPadding" instead as GCM does not require any padding.

To fix the problem, use ivBytes instead of ivSend. Hexadecimals and base 64 are textual representations of binary. The IV itself should consist of 12 binary bytes, not 16 bytes containing base 64 in ASCII.

Note that for GCM to be secure the "IV" should be a unique value, i.e. a so called nonce. Otherwise you'd have to use different keys for each encryption.