Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#195·crypto-js

Help Needed: How to decrypt AES 256 CBC with IV

Author: BrodaNoelCreated Dec 24, 2018Updated May 18, 2026

Hi guys. First of all, sorry for this question-issue, but I spent more than 5 hours and my brain in getting burned.

Look, in Node I'm encrypting using this function:

javascript
const IV_LENGTH = 16;
const SECRET = '12345678901234567890123456789012'; // 32 chars

function encrypt(text) {
  let iv = crypto.randomBytes(IV_LENGTH);
  let cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(SECRET), iv);
  let encrypted = cipher.update(text);
  
  encrypted = Buffer.concat([encrypted, cipher.final()]);

  return iv.toString('hex') + ':' + encrypted.toString('hex');
}

So, for example, if I call this function in this way: encrypt('noel'), I get:

39f491a6ee47bec56e237f9f54b30658:e478300762f3a8b980fa94ecda0dd1a8

Then, the decrypt functions do:

javascript
const IV_LENGTH = 16;
const SECRET = '12345678901234567890123456789012'; // 32 chars

function decrypt(text) {
  let textParts = text.split(':');
  let iv = new Buffer(textParts[0], 'hex');
  let encryptedText = new Buffer(textParts[1], 'hex');
  
  let decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(key), iv);
  let decrypted = decipher.update(encryptedText);

  decrypted = Buffer.concat([decrypted, decipher.final()]);

  return decrypted.toString();
}

So, as you can see, the decrypt function split the string using :. the element 0 is the IV and the element 1 is the encrypted text.

Now guys, due to my frontend is a mobile App, I have to re-write the decrypt function (because crypto doesn't exist). I have this, but it's not working at all. Can you please help me? What I'm missing?

javascript
const SECRET = '12345678901234567890123456789012'; // 32 chars
function decrypt(text) {
  const textParts = text.split(':');
  const iv = textParts[0];
  const encryptedText = textParts[1];

  const result = crypto.AES.decrypt(
    crypto.enc.Hex.parse(encryptedText),
    crypto.enc.Hex.parse(SECRET),
    {
      iv: crypto.enc.Hex.parse(iv),
      mode: crypto.mode.CBC
    }
  ).toString();

  console.log('result', result);

  return result;
}

Source: brix/crypto-js

View original on GitHubView discussion on GitHub