0
votes

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
Your _text buffer is only 11 bytes long. ECB consumes 16 bytes. A good library would either (language allowing) not compile with this due to static checks or throw an exception during run time. - Thomas M. DuBuisson
@ThomasM.DuBuisson+OP: nodejs crypto uses OpenSSL, apparently the EVP_Cipher* routines, which by default do PKCS5/7 padding for modes that need it like ECB, and thus can correctly handle any data length. - dave_thompson_085
So the operation performed is not what is expected, also not great. - Thomas M. DuBuisson
yep.. indeed I'm searching for an easy to use yet better aes c++ implementation - alfredopacino

1 Answers

1
votes

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.