If you need to use AES with ECB encryption mode and PKCS5Padding padding in node, this post is for you. If not, it will be indexed by google!
The AES/ECB/PKCS5Padding encryption method is common in JAVA, Coldfusion, ... when no initialisation vector (IV) is provided.
The following code will apply the right aes algorithm depending on the bit size of the provided key.
"use strict"
var crypto = require 'crypto'
var encrypt = function(key, content) {
// be sure you pass in the right encoding for the key!
var encryptionKeyBuf = new Buffer(key, "base64");
// get the AES algorithm version from the key size
var keySizeBits = encryptionKeyBuf.length * 8;
var aesAlgorithm = "AES-" + keySizeBits + "-ECB";
// because PKCS5Padding uses 8-byte block sizes
var ivBuf = new Buffer(0);
var cipher = crypto.createCipheriv(
aesAlgorithm,
encryptionKeyBuf,
ivBuf);
return [
cipher.update(content, "utf8", "hex"),
cipher.final("hex")
].join('')
}
Oscar Out.