Sign in to follow this  
Followers 0
DeltaRocked

CrossPlatform : NodeJS / AutoIT - AES Encryption

3 posts in this topic

Hi Everyone,

I have been breaking my head over this NodeJS / AutoIT AES Encryption Decryption. String encrypted by AutoIT needs to be decrypted by NodeJS using AES. However there seems to be some issue the way NodeJS has implemented AES.

AutoIT Code:

#include "aes.au3"
$line1 = 'Test'
$Key = "YoYo"
$Mode = "CBC"
$IV = "0000000000000000"
$ret = _AesEncrypt($Key, $line1, $Mode, $IV)
$a = _AesDecrypt($Key, $ret, $Mode)
ConsoleWrite('Tobe: '&$line1 & @CRLF & "Encrypted: "&$ret & @CRLF &"Decrypted: " & BinaryToString($a) & @CRLF)

Encrypted: 0x3030303095E8819AAA0BD7A94E88DA291B62EE52
Decrypted: Test

NodeJS Code:

var crypto = require('crypto');
var iv = new Buffer('0000000000000000');
var encrypt = function(data, key) {
    var decodeKey = crypto.createHash('sha256').update(key, 'binary').digest();
    var cipher = crypto.createCipheriv('aes-256-cbc', decodeKey, iv);
    return cipher.update(data, 'binary', 'hex') + cipher.final('hex');
};

var decrypt = function(data, key) {
    var encodeKey = crypto.createHash('sha256').update(key, 'binary').digest();
    var cipher = crypto.createDecipheriv('aes-256-cbc', encodeKey, iv);
    return cipher.update(data, 'hex', 'binary') + cipher.final('binary');
};

var data = 'Test';
var key = 'YoYo';
var cipher = encrypt(data, key);
var decipher = decrypt(cipher, key);
console.log('Encrypted: ',cipher);
console.log('Decrypted: ',decipher);

Encrypted:  221335caa771fea2a15d7ec303e4bb2d
Decrypted:  Test

Am just unable to understand , what I should be doing in this regard, so as get the same encrypted string on both the platforms.

Regards

Deltarocked

Share this post


Link to post
Share on other sites



Whatever mess I had done was done ... now for the clean code.

However, the Node.JS encrypted output for input strings > 16 doesnt seem to match up with that of AutoIT

#include "rijndael.au3"
#include <String.au3>

Global $key = "1234567812345678"
Global $text = "abcdabcdabcdabc"

$encrypted = _StringToHex(BinaryToString(_rijndaelCipher($key, $text, 128, 0, '','0')))
ConsoleWrite("Encrypted: " & $encrypted & @CRLF)
$decrypted = BinaryToString(_rijndaelInvCipher($key, _HexToString($encrypted), 128, 0, '','0'))
ConsoleWrite("Decrypted: " & $decrypted & @CRLF)

OutPut AutoIT :

Encrypted: 63B029D7F7134F055C3309F99CE09215
Decrypted: abcdabcdabcdabc

Node.JS :

var crypto = require('crypto');
aesEncryptString('abcdabcdabcdabc', '1234567812345678', crypto);


function aesEncryptString(input1, key1, crypto) {
    var CIPHER_METHOD = "aes-128-cbc";
    var KEY_LENGTH = 16;

    var AES_BLOCK_SIZE = 16;
    var AES_PAD_STARTER = new Array(16).join('\0');

    var key = new Buffer(key1.substring(0, KEY_LENGTH),'ascii');
    var iv = new Buffer(16);
    iv.fill(0);

    var input = new Buffer(input1,'ascii');
    var aesCipher = crypto.createCipheriv(CIPHER_METHOD, key, iv);
    var plainText = input;
    var padLength = 0;
    if ((input.length % AES_BLOCK_SIZE) === 0) {
        padLength = 0;
    } else {
        padLength = AES_BLOCK_SIZE - (input.length % AES_BLOCK_SIZE);
    }

    var output;

    aesCipher.setAutoPadding(false);
    console.error('InputLength : '+input.length,'MOD: ' + input.length % AES_BLOCK_SIZE,'padLength: '+padLength);

    input += AES_PAD_STARTER.substr(0, padLength);

    console.error('InputLength : '+input.length,'Input : '+ input);
    output = aesCipher.update(input, 'utf8', 'hex') + aesCipher.final('hex');

    console.error('IV         : ' , iv);
    console.error('Key        : ' , key);
    console.error('Plaintext  : ' , plainText);
    console.error('Ciphertext : ' , output.toUpperCase());

    var DaesCipher = crypto.createDecipheriv(CIPHER_METHOD, key, iv);

    DaesCipher.setAutoPadding(false);

    output = DaesCipher.update(output, 'hex', 'ascii') + DaesCipher.final();

    console.error('Decrypted : ',stripchars(output,'\0'));
    console.error('---------------_____-------------------');
}

function stripchars(string, chars) {
      return string.replace(new RegExp('['+chars+']','g'), '');
    }

OUTPUT of NODE.JS:

InputLength : 15 MOD: 15 padLength: 1
InputLength : 16 Input : abcdabcdabcdabc //terminated by null
IV : <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
Key : <Buffer 31 32 33 34 35 36 37 38 31 32 33 34 35 36 37 38>
Plaintext : <Buffer 61 62 63 64 61 62 63 64 61 62 63 64 61 62 63>
Ciphertext : 63B029D7F7134F055C3309F99CE09215
Decrypted : abcdabcdabcdabc

Now for the Erroneous Output.

Autoit Output : 
StringLen : 17
Encrypted: D374CCD51195D5AFE33537E909A331E7D477B6BA4A582E3C68B43C42064F6218
Decrypted: abcdabcdabcdabcde

Node.JS
InputLength : 17 MOD: 1 padLength: 15
InputLength : 32 Input : abcdabcdabcdabcde
IV         :  <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
Key        :  <Buffer 31 32 33 34 35 36 37 38 31 32 33 34 35 36 37 38>
Plaintext  :  <Buffer 61 62 63 64 61 62 63 64 61 62 63 64 61 62 63 64 65>
Ciphertext :  D374CCD51195D5AFE33537E909A331E7127F4D3EA6A5C8200A49E7DA9ACAB758
Decrypted :  abcdabcdabcdabcde

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0