I’m trying to get the data decoded using Node native methods, I search how to achieve this but it get invalid iv initialization or Unsupported state or unable to authenticate data trying differents iv
- aes-128 -> 16 bytes
it results in ->
{ "statusCode": 500,
"message": "Invalid initialization vector" }
let decoded: string = '';
for (let ndx = 0; ndx < hash.length; ndx += 2) {
let i = parseInt('0x' + hash.substring(ndx, ndx + 2));
decoded += String.fromCharCode(i);
}
try {
const password = 'somePassword';
const key = crypto.scryptSync(password, 'salt', 16);
const iv = Buffer.alloc(11, password, 'ascii'); // Initialization vector.
const iv2 = crypto.randomBytes(32)
const iv3 = iv2.toString('hex').slice(0, 16);
const iv4 = Buffer.allocUnsafe(32)
const decipher = crypto.createDecipheriv('aes-128-gcm', key, iv);
let decrypted = decipher.update(decoded, 'ascii', 'utf8');
decrypted += decipher.final('utf8');
I’tried many iv but all of them results in ->
Unsupported state or unable to authenticate data
2
Answers
AES always has a block size of 128. So if we’re talking about AES-128, -192 or -256 then the number indicates the key size in bits.
So if you specify
aes-128-ccm
then the expected key size is 128 / 8 = 16 bytes. Either you’ll have to specifyaes-192-ccm
or the key size needs to be 16 rather than 24 in yourscryptSync
function. Which one is correct depends of course on how the encryption is performed.