-1

I would like to rewrite following python code to node.js. I'm gonna rewrite small crypto/decrypto library with AES algorithm. I want to decrypto values encrypted by python crypto function.

Then i wrote codes in node.js, but it's still something wrong.. :(

If it's possible, please look at this. thank you.

Python

import hashlib
imoprt base64
from Crypto.Cipher import AES
from Crypto import Random

# secret: the raw key used by the algorithm
# data: the data to encypt

m = hashlib.md5()
m.update(secret)
key = m.hexdigest()

iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_ECB, iv)
encrypted = base64.b64encode(iv + cipher.encrypt(pad((data))))

bdata = base64.b64decode(encrypted)
iv = bdata[0:AES.block_size]
edata = bdata[AES.block_size:]
cipher = AES.new(key, AES.MODE_ECB, iv)
decrypted = cipher.decrypt(edata).strip("\0")

# data == decrypted should be true

def pad(self, s):
  return s + (16 - len(s) % 16) * "\0"

node.js

var crypto = require('crypto');

var AES_BLOCK_SIZE = 16;
var algorithm = 'aes-256-ecb';
var inputEncoding = 'utf8';
var outputEncoding = 'base64';

var m = crypto.createHash('md5');
m.update(secret);
var key = m.digest('hex');

var iv = crypto.randomBytes(AES_BLOCK_SIZE);
var cipher = crypto.createCipheriv(algorithm, key, iv);
var ciphered = cipher.update(data, inputEncoding, 'binary');
ciphered += cipher.final('binary');
var encrypted = Buffer.concat([iv, new Buffer(ciphered, 'binary')]);
encrypted = encrypted.toString(outputEncoding);

var buffer = new Buffer(encrypted, outputEncoding);
var iv = buffer.slice(0, AES_BLOCK_SIZE);
var edata = buffer.slice(AES_BLOCK_SIZE, buffer.length);

console.log(key.length); # 16
console.log(iv.length); # 16

var decipher = crypto.createDecipheriv(algorithm, key, iv);
decipher.setAutoPadding(false);
var deciphered = decipher.update(edata, 'binary', inputEncoding);

# it says "node-crypto : Invalid IV length"

also I tried to rewrite decipher part of node.js. still doesn't work and got an error...

var decipher = crypto.createDecipheriv(algorithm, key, iv);
var buffers = [];
buffers.push(decipher.update(edata));
buffers.push(decipher.final(inputEncoding));
var plaintext = Buffer.concat(buffers).toString(inputEncoding);

TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
gevorg
  • 4,835
  • 4
  • 35
  • 52
toshipon
  • 53
  • 1
  • 5

1 Answers1

0

finally, I fixed that by myself.. :-p so basically, ecb mode wasn't necessary iv. then i putted blank text into iv parameter.

var m = crypto.createHash('md5');
m.update(secret);
var key = m.digest('hex');

var iv = crypto.randomBytes(AES_BLOCK_SIZE);
var cipher = crypto.createCipheriv(algorithm, keyBuf, '');
cipher.setAutoPadding(true);
var ciphered = cipher.update(expect, inputEncoding, 'binary');
ciphered += cipher.final('binary');
var encrypted = Buffer.concat([iv, new Buffer(ciphered, 'binary')]);
var edata = encrypted.toString(outputEncoding);

var decipher = crypto.createDecipheriv(algorithm, key, '');
decipher.setAutoPadding(false);
buffers.push(decipher.update(edata, 'binary'));
buffers.push(decipher.final('binary'));
var decrypted = buffers.join('');
var plaintext = decrypted.toString('utf8');
toshipon
  • 53
  • 1
  • 5