crypto wrong AES-256-ecb encrypt

南楼画角 提交于 2020-01-06 04:56:07

问题


I tried to encrypt "aaaaaaaaaaa" in aes-256-ecb

var encrypt = function(cryptkey, cleardata) {
     var encipher = crypto.createCipher('aes-256-ecb', cryptkey);
     return Buffer.concat([
          encipher.update(cleardata),
          encipher.final()
        ]);
 }       
var hex_key = [0x2A,0x46,0x29,0x4A,0x40,0x4E,0x63,0x52,0x66,0x55,0x6A,0x58,0x6E,0x32,0x72,
    0x35,0x75,0x38,0x78,0x2F,0x41,0x3F,0x44,0x28,0x47,0x2B,0x4B,0x61,0x50,0x64,0x53,0x67]
var _text =  new Buffer([0x61,0x61,0x61,0x61,0x61,0x61,0x61,0x61,0x61,0x61,0x61])
console.log(encrypt(hex_key,_text));

//prints
<Buffer 2c 2d 90 b3 0f ef 3d 6f 73 68 db da 72 34 9c 80>

I noticed it doesn't match the result of this encryption tool http://aes.online-domain-tools.com/link/10a8ee2gzyiHxWlCJr/

Edit: Eventually I read the doc and get that signature mistake (as Maarten Bodewes suggests) now I'm in doubt about the output:

node crypto using createCipheriv() AND pyCrypto (python) output:

8f c9 89 36 ba 7b 16 2a a8 bc 11 a4 b4 cd e3 08

online tool AND a c++ library:

ad 5f 91 18 2c ed d1 d1 db 0d ab 34 8c 1c 8b

Who's right?

SOLUTION: Eventually I got it, the c++ library and that online tool uses 0-padding method to fill the block. Not sure what's the default way to handle padding in node crypto and pyCrypto, anyway that's why I got 2 different cipher text from 4 aes implementations.


回答1:


ECB mode doesn't take an IV. However, if you leave it out you will match the method signature for this createCipher method that takes a password instead of a key. So you'll need some object representing an IV to choose the method that takes a key instead of a password, even if that object is subsequently ignored.

Don't test your code against some crappy online tool, use the NIST test vectors instead. Don't use ECB. And please, with sugar on top, make sure you read the API documentation carefully, especially after you've run into problems.



来源:https://stackoverflow.com/questions/48510385/crypto-wrong-aes-256-ecb-encrypt

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