forge icon indicating copy to clipboard operation
forge copied to clipboard

Unable to decrypt AES-GCM content

Open lkachun9129 opened this issue 6 years ago • 2 comments

I was trying to decrypt a string which is encrypted using AES/GCM/NoPadding in Java.

However, I couldn't decrypt the content correctly. The output string was corrupted.

On the other hand, I encrypted a string in node-forge using AES-GCM and tried to decrypt in Java. The decryption was failed with an exception javax.crypto.AEADBadTagException: Tag mismatch!.

Here is my configuration:

JAVA AES 256 Key generated by PDKBF2 (PBKDF2WithHmacSHA1) with 1000 iterations GcmParameter with a 12-byte IV and tag length set to 128 bits

Typescript

// key generation
const salt = NodeForge.util.hexToBytes('00000000000000000000000000000000');
const iv = NodeForge.util.hexToBytes('000000000000000000000000');
const key = NodeForge.pkcs5.pbkdf2('password', salt, 1000, 32);


// encryption
const cipher = NodeForge.cipher.createCipher('AES-GCM', key);
cipher.start({ iv: iv, tagLength: 128 });
cipher.update(NodeForge.util.createBuffer('aaa'));
cipher.finish();

let fullOutput = '' + cipher.output.bytes() + cipher.mode.tag.bytes();
let decodedData = NodeForge.util.encode64(fullOutput);


// decryption
let bytesBuffer = NodeForge.util.createBuffer(decodedData);
let actualData = NodeForge.util.createBuffer(bytesBuffer.getBytes(bytesBuffer.length() - 16));
let tag = NodeForge.util.createBuffer(bytes.substring(bytes.length - 16));

const decipher = NodeForge.cipher.createDecipher('AES-GCM', key);
decipher.start({ iv: iv, tagLength: 128, tag: tag });
decipher.update(actualData);
decipher.finish();

Any idea?

lkachun9129 avatar May 27 '20 15:05 lkachun9129

After checking the logic on Java side, we discovered an issue. Now the problem has been fixed.

lkachun9129 avatar May 28 '20 02:05 lkachun9129

After checking the logic on Java side, we discovered an issue. Now the problem has been fixed.

@lkachun9129 could you share some info what was the issue on the java side? thnx!

robertnosko avatar Aug 21 '20 05:08 robertnosko