How does rewrite python crypto code to node.js? -


i rewrite following python code node.js. i'm gonna rewrite small crypto/decrypto library aes algorithm. want decrypto values encrypted python crypto function.

then wrote codes in node.js, it's still wrong.. :(

if it's possible, please @ this. thank you.

python

import hashlib imoprt base64 crypto.cipher import aes crypto import random  # secret: raw key used algorithm # data: data 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 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);  # says "node-crypto : invalid iv length" 

also tried rewrite decipher part of node.js. still doesn't work , got 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 

finally, fixed myself.. :-p basically, ecb mode wasn't necessary iv. putted blank text 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'); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -