Skip to content

Instantly share code, notes, and snippets.

@gimelfarb
Last active July 2, 2025 18:43
Show Gist options
  • Select an option

  • Save gimelfarb/da6264e5ee4348c11450879258641580 to your computer and use it in GitHub Desktop.

Select an option

Save gimelfarb/da6264e5ee4348c11450879258641580 to your computer and use it in GitHub Desktop.
CryptoJS AES encryption with custom Key & IV
const CryptoJS = require('crypto-js');
const msg = CryptoJS.enc.Hex.parse('00010203');
// key & iv - 128-bit (16 byte)
const key = CryptoJS.enc.Hex.parse('1234567890abcdef1234567890abcdef');
const iv = CryptoJS.enc.Hex.parse('fedcba0987654321fedcba0987654321');
// AES-128
const enc = CryptoJS.AES.encrypt(msg, key, {
iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
console.log(enc.ciphertext.sigBytes);
console.log(enc.ciphertext.toString(CryptoJS.enc.Hex));
// Output: ae1e5a28075afe89c82967bed75e52b4
// Check: https://cryptii.com/pipes/dgvLxQ
@chris6611
Copy link

Hello,

I am trying to use this but with AES-256. Isn't this a 256 bit key and IV since its 32 chars in length?

@gimelfarb
Copy link
Author

Isn't this a 256 bit key and IV since its 32 chars in length?

Well, 32 hex chars = 16 bytes (2 hex chars per byte) = 128 bit
Consequently, 256-bit key would be 64 characters in hex encoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment