Encrypt binary data with aes-ecb on node.js

别等时光非礼了梦想. 提交于 2019-12-24 03:35:11

问题


I try to do crypto on node.js but badly I fail to have the same result than online sites.

I want to encrypt some binary data with a binary key. I use the tutorial on nodejs site but I have a different result from my reference data set. My reference data set is validated with java code, with C code and with two online site : http://aes.online-domain-tools.com/ and https://www.hanewin.net/encrypt/aes/aes-test.htm

Have you an idea how to encrypt the same way that those sites? I guess it can be the padding?

Thanks in advance. François

My reference data set :

    key=8CBDEC62EB4DCA778F842B02503011B2
    src=0002123401010100000000000000c631
    encrypted=3edde3f1368328a1a37cf596bc8d4a7c

My code :

    var key = new Buffer('8CBDEC62EB4DCA778F842B02503011B2', 'hex')
    var src = new Buffer('0002123401010100000000000000c631', 'hex')
    cipher = crypto.createCipher("aes-128-ecb", key)
    result = cipher.update(src).toString('hex');
    result += cipher.final().toString('hex');
    "result   : " + result

Output :

    result   : 4da42b57b99320067979086700651050e972f1febd1d506e5c90d3b5d3bc9424

回答1:


Thank you Artjom B.

I post hereunder the fixed code :

var key = new Buffer('8CBDEC62EB4DCA778F842B02503011B2', 'hex')
var src = new Buffer('0002123401010100000000000000c631', 'hex')
cipher = crypto.createCipheriv("aes-128-ecb", key, '')
cipher.setAutoPadding(false)
result = cipher.update(src).toString('hex');
result += cipher.final().toString('hex');
"result   : " + result

To decrypt, do the same :

var key = new Buffer('8CBDEC62EB4DCA778F842B02503011B2', 'hex')
var encrypted = new Buffer('3edde3f1368328a1a37cf596bc8d4a7c', 'hex')
decipher = crypto.createDecipheriv("aes-128-ecb", key, '')
decipher.setAutoPadding(false)
result = decipher.update(encrypted).toString('hex');
result += decipher.final().toString('hex');
"result   : " + result

Thanks, i am sincerely grateful. Regards, François



来源:https://stackoverflow.com/questions/43788513/encrypt-binary-data-with-aes-ecb-on-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!