Recreating MCRYPT_RIJNDAEL_128 in node.js

Trying to recreate the following php encryption code in node.js:

$size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($size, MCRYPT_RAND);
$msg = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, 'MY_KEY_LONG_STRING', 'PLAINTEXT', MCRYPT_MODE_ECB, $iv));

Tried this:

var text = 'PLAINTEXT';
var len = text.length;
for (var i = 0; i < 16 - len % 16; i++) {  // pad to multiple of block size 
    text += '\0';
}
var key = 'MY_KEY_LONG_STRING';
key = key.substr(0, 16); // trim to expected key size for aes128

var cipher = crypto.createCipher('aes-128-ecb', key);
cipher.setAutoPadding(false); // did our own padding, to match mcrypt_encrypt
var encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');

Getting different result from the php one...

Also tried creating cipher with IV (which shouldn't even be used in aes-128-ecb):

var cipher = crypto.createCipheriv('aes-128-ecb', key, '');

Also, different result from php. Any ideas how to make this behave exactly like the php version?


AES Encrypt data in php and decrypt in node js. · GitHub, Trying to recreate the following php encryption code in node.js: $size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $iv  Recreating MCRYPT_RIJNDAEL_128 in node.js. javascript,php,node.js,aes,mcrypt. Playing two rather ill constructed libraries against each other can be fun. Rather than


Here is my code to solve problem with migration from PHP to NodeJS. I have strict 32 bytes key so I have to use aes-256-ecb.

PHP code I want rewrite:

$text = "Some super mega text I want to encode";
$skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";

function encode($text,$skey) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $skey, $text, MCRYPT_MODE_ECB, $iv);
    return base64_encode($crypttext);
}

echo encode($text,$skey);

Output:

dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD

NodeJS:

var crypto = require('crypto');

var text = "Some super mega text I want encode";
var skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";

function encode(text, skey) {
    var len = text.length;
    var padSize = 16 - ((len + 16 - 1) % 16 + 1);
    for (var i = 0; i < padSize; i++) { 
        text += '\0';
    }
    var cipher = crypto.createCipheriv('aes-256-ecb', skey, '');
    cipher.setAutoPadding(false);
    var encrypted = cipher.update(text, 'utf8', 'base64');
    encrypted += cipher.final('base64');
    return encrypted;
}

console.log(encode(text, skey));

Output:

dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD

Also NodeJS mcrypt package works:

npm install mcrypt

Code:

var text = "Some super mega text I want encode";
var skey = "rcbTw667C7zxghZ5U3gwhQlp22t8c5Rq";

function encode(text, skey) {
    var MCrypt = require('mcrypt').MCrypt;
    var rijEcb = new MCrypt('rijndael-128', 'ecb');
    rijEcb.open(skey);
    var ciphertext = rijEcb.encrypt(text);
    return ciphertext.toString('base64');
}

console.log(encode(text, skey));

Output:

dU1eOy+YwkYvm/KCTB8aqR1UsisyrHrvBu+E/H3G/s0aagMDKlNFekGXNQyFMFJD

Convert Encrypt / Decrypt PHP function to Node JS, creating cipher object using Rijndael encyrption algorithm with Cipher-block chaining we will be decrypting data stored in $cipherHexText256 from node js. */. Rijndael-JS. This is an implementation of Rijndael algorithm. Supports 128/192/256 bit key/block, and ECB, CBC modes. Unlike node-rijndael, or node-mcrypt, this is an pure-JS implementation.


I tried other option but nothing worked but This worked for me.

function encrypt(text) {
  text = '' + text;
  var crypto = require('crypto');
  var len = text.length;
  var padSize = 16 - (((len + 16 - 1) % 16) + 1);
  var data = String.fromCharCode(padSize);
  var text = text + data.repeat(padSize);

  var cipher = crypto.createCipheriv('aes-128-ecb','secretKey', '');
  cipher.setAutoPadding(false);
  var encrypted = cipher.update(text, 'utf8', 'base64');
  encrypted += cipher.final('base64');
  return encrypted;
}

Similer in PHP

function aes128Encrypt($str,$key){
$block = mcrypt_get_block_size('rijndael_128', 'ecb');
$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB));
}

AES Between Python, Go, Javascript(NodeJS) and PHP – Dmyz, Make this functions easy to use by creating a module so I can just do I have no time to do it so I need someone to convert two functions from PHP code to Node JS. MCRYPT_RIJNDAEL_128 is AES-compliant, and MCRYPT_MODE_CBC. MCrypt bindings for Node.js. Contribute to tugrul/node-mcrypt development by creating an account on GitHub.


Crypto, Named `MCRYPT_RIJNDAEL_128` in PHP5, Nodejs/PHP7.1 is aes-256-cbc. Use CBC mode due to ECB no IV. Key is `  AES Encrypt data in php and decrypt in node js. . GitHub Gist: instantly share code, notes, and snippets.


Stream, Class: DiffieHellman #. Added in: v0.5.0. The DiffieHellman class is a utility for creating Diffie-Hellman key exchanges. return bin2hex (mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $ this-> encryptKey, I gave a try encoding it using the node.js uft8 lib but that failed.


Decrypt javascript, The stream module is useful for creating new types of stream instances. It is usually not necessary to use the stream module to consume streams. Organization of  All variants of AES use a 128-bit block size with varying key lengths (128, 192, or 256). This means that MCRYPT_RIJNDAEL_128 is the only correct choice if you want AES. MCRYPT_RIJNDAEL_192 and MCRYPT_RIJNDAEL_256 instead refer to non-standard, less-studied variants of the Rijndael block cipher that operate on larger blocks.