Node.js AES decryption

I have some decryption code in python that I need ported to node. I cannot seem to get the decryption right and I am going crazy.

My python code is:

from Crypto.Cipher import AES

mode = AES.MODE_ECB
secret = "9kL8yb/3Tu2czOr5qfiGPgJmx25s+T15"
cipher = AES.new(secret, mode)

DecodeAES = lambda c, e: c.decrypt(e.decode("hex"))
incoming = '813411aa65dcb66802a6e4d5995d8302'

pre_add = DecodeAES(cipher, incoming)
print pre_add

The result of the incoming is 0004a38bc14e7533 which is correct.

I am sorry I am very new to node and javascript.

I need to somehow get the exact same thing working from node.js

I have the following for node so far:

var crypto = require('crypto');


var ciphertext = '813411aa65dcb66802a6e4d5995d8302';
var key = '9kL8yb/3Tu2czOr5qfiGPgJmx25s+T15';
var binkey = new Buffer(key, 'binary');


var decipher = crypto.createDecipher('aes-256-ecb', binkey, '');
decipher.setAutoPadding(auto_padding=true);

var dec = decipher.update(ciphertext, 'hex');
dec += decipher.final();

Another update from my understanding of the docs

Please help I am getting desperate.

Many thanks

Jonny

Nobody is going to just port your Python code to JavaScript. Especially your bizarrely non-Pythonic Python code (why would you ever do foo = lambda blah instead of just def foo(blah)?).

But here's how to get started:

Node.js comes with a module called crypto that is, not too surprisingly, similar in functionality to the PyCrypto module you're using.

The Node module has separate objects for ciphering and deciphering, unlike Python. The function to create a cipher is called crypto.createCipher, and to create a decipher is crypto.createDecipher.

As the documentation says, the objects you get back from those functions are read-write streams. You write encrypted data to the Decipher stream, and read decrypted data.


Meanwhile, this ridiculously silly line of Python code:

DecodeAES = lambda c, e: c.decrypt(e.decode("hex"))

… is probably easier to read if you write it like this:

def DecodeAES(cipher, hexdata):
    bindata = hexdata.decode("hex")
    return cipher.decrypt(bindata)

You may notice that it does two things to the incoming hex data. But your attempted equivalent Node code only does one of those two things. That isn't likely to work.


In your new version of the Node code, you do this:

var BinCipher = new Buffer(ciphertext, 'binary');

That's apparently supposed to do the same thing as the Python:

bindata = hexdata.decode("hex")

It obviously doesn't, because you didn't put the hex anywhere. Try printing it out to see what you get:

> console.log(BinCipher)
<Buffer 65 35 38 31 61 63 62 38 33 30 30 64 64 38 34 31 38 33 33 30 30 39 64 65 36 37 33 36 37 35 39 33>

Clearly, that 65 is the letter e, the 35 is the digit 5, etc. You haven't unhexlified anything. Compare to the Python version:

>>> print repr(bindata)
'\xe5\x81\xac\xb80\r\xd8A\x830\t\xdeg6u\x93'

Here, the first character is \xe5, the next is \x81. That's what you want.

And I'm not even sure why you think you need a Buffer in the first place. If you read the docs I linked to above, they clearly show that you can pass "'binary', 'base64', or 'hex'" input and output encodings straight to the Decipher methods.