skip to Main Content

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

  1. 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


  1. 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 specify aes-192-ccm or the key size needs to be 16 rather than 24 in your scryptSync function. Which one is correct depends of course on how the encryption is performed.

    Login or Signup to reply.
  2.     const crypto = require('crypto');
    
    const encryptedData = '<your_encrypted_data>'; 
    const iv = Buffer.from('<your_iv>', 'base64'); 
    
    
    const password = 'somePassword';
    const key = crypto.scryptSync(password, 'salt', 16); // 16 bytes key for AES-128
    
    const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
    
    decipher.setEncoding('utf8');
    
    let decrypted = decipher.update(encryptedData, 'base64', 'utf8');
    decrypted += decipher.final('utf8');
    
    console.log(decrypted);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search