AES decryption with crypto

947 views
Skip to first unread message

Jonny_Flowers

unread,
Jul 2, 2013, 6:18:40 PM7/2/13
to nod...@googlegroups.com
Hi,
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"))
pre_add = DecodeAES(cipher, incoming)
I need to somehow get the exact same thing working from node.js

Please help I am getting desperate.

Many thanks
Jonny

Tauren Mills

unread,
Jul 3, 2013, 3:10:32 AM7/3/13
to nod...@googlegroups.com

I recently did the something similar, but going from java to node. Perhaps my blog post will prove mildly helpful to you.

http://tauren.com/2013/01/decrypt-java-shiro-rememberme-cookie-in-node-js-express-app/

I believe I made some improvements to the node.js code since that post, let me know if it would help and I'll dig them up for you.

Tauren

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kelsey Dawes

unread,
Jul 3, 2013, 12:18:38 PM7/3/13
to nod...@googlegroups.com
Hi, 

As usual, the thing to do in these cases is to get a very small example working and then extend it to match your full  /  final use case.

I don't know the python crypto stuff and it's not obvious from the snippet you posted which AES/ECB cipher you are using.   This could be the source of your problems.
See : 

crypto.getCiphers()

Returns an array with the names of the supported ciphers.

--
Anyway, here is a short example using aes-128-ecb  and your key :
---- Encode :
var cipherType = "aes-128-ecb";
var cipherKey  = "9kL8yb/3Tu2czOr5qfiGPgJmx25s+T15";                                                                                     
var ecb = new Buffer("hello there");
var cipher = crypto.createCipher(cipherType, cipherKey);

var t = [];cipher.setAutoPadding(true);t.push(cipher.update(ecb, "ascii", "hex"))t.push(cipher.final("hex"));
console.log(t.join(''));
-----Decode : 
var fs = require('fs'),
        crypto = require('crypto');

var cipherType = "aes-128-ecb";
var cipherKey  = "9kL8yb/3Tu2czOr5qfiGPgJmx25s+T15";
var ecb = fs.readFileSync("./encrypted.txt", "ascii").trim();
var decipher = crypto.createDecipher(cipherType, cipherKey);

var t = [];
t.push(decipher.update(ecb, "hex", "ascii"));
t.push(decipher.final("ascii"));

console.log(t.join(''));
----
$ node ./s_enc.js
710c90af43621e6c9b6d7da36d2c17f1
$ node ./s_enc.js > encrypted.txt
$ node ./s_dec.js
hello there

- Kelsey




--
Reply all
Reply to author
Forward
0 new messages