ricmoo / aes-js

A pure JavaScript implementation of the AES block cipher and all common modes of operation for node.js or web browsers.
MIT License
1.45k stars 272 forks source link

Encrypted Hex differs from Node #47

Open aborigene opened 6 years ago

aborigene commented 6 years ago

I am using the library on a client connecting to a node backend. The problem is that I am receiving an error Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt.

I have checked and the same data input with same iv and same key, generates different encrytped hex on both sides. What catches my attention is that in one of the cases the hex usin AESJs on the client is much smaller than the one from Node.

Encrypting and decrypting on Node (isolated) and on the client run fine, the problem is when encrypt on the client and send to Node.

Is there any mode I should change that I am not aware of? I have tried all the IV initiaded modes with no luck. Below some examples: Input data1: 'este é um teste'
Encrypted Hex1 Node: 0b247a5b635d84639cc02722bd8257f256bc2fd7b12fcacdc8d082e9f6951bf7
Ecnrypted Hex1 Client: 554fb689bca935a2a92278739c23d353Comments Input data2: '{\"CPF\":\"29278804843\",\"action\":\"verificarCPF\",\"nonce\":\"6f1ae96b7ba43d4f1985fa37d\"}' Encrypted Hex2 Node: c575af80bde78b8040aa5b4d37ff0204eba68fcfbe004453e9b41e964303ee2fac43ff50bdd2e62a893b34fbeef3731c573672934640383818eefc77e1f052f7eec9795ee47465e249159f529e14599ff85f296c4e67fc3bcbde89af969b6d52 Encrypted Hex2 Client: e340c06df883d4dacf9fe1a814dabee8ab5eaf39728ad2544348f60aa5909f78a0fdb3d8f3c17247ed37bed559b702788d6a6ed5029c768179415a918a771b49833047def73e97e5453fcd931241104b7e697668f4c66f359ee8b48ff418f1ff

Regards,

ricmoo commented 6 years ago

Which mode of operation are you using? Is there any padding being done by the other library?

Can you provide an example for both the other library and aes-js with:

If you don't know some of the above, I may be able to figure it out from the others. Thanks. :)

aborigene commented 6 years ago

ricmoo,

Please see below the information: key = "4847afd2c068b1644d47709892bde97d" iv = "a005f0cb74cb344427e545894bdec41a" Mode of operation = CFB padding = pkcs7 text being encrypted = "this is a test" Below a code snipet of what I am doing:

var aesCfb2 = new aesjs.ModeOfOperation.cfb(Buffer(finalSecret, 'hex'), iv);
var encryptedBytes2 = aesCfb.encrypt(aesjs.padding.pkcs7.pad(aesjs.utils.utf8.toBytes("this is a test")));
var encryptedHex2 = aesjs.utils.hex.fromBytes(encryptedBytes2);
alert("Simple encrypted-CFB: "+encryptedHex2);

The encrypted hex is: 0bbcfccb51ad9937c7ae31aa349a98f297dcef4e6b7434898d72f01265011542 I have tried several mode of operations and none of them match the encryption on Node.

On Node I am using aes128 with the default settings, including auto padding (pkcs7).

Thanks a lot for your help on this.

Regards,

ricmoo commented 6 years ago

@aborigene

Is there maybe something going on with your Buffer class in the browser?

I get:

var key = "4847afd2c068b1644d47709892bde97d"
var iv = "a005f0cb74cb344427e545894bdec41a"
var aesCfb2 = new aesjs.ModeOfOperation.cfb(aesjs.utils.hex.toBytes(key), aesjs.utils.hex.toBytes(iv));
var encryptedBytes2 = aesCfb2.encrypt(aesjs.padding.pkcs7.pad(aesjs.utils.utf8.toBytes("this is a test")));
console.log(aesjs.utils.hex.fromBytes(encryptedBytes2));
"92341e8fd232ccffca3cff8151da626f"

Is that what you get in node?

zhaoxiongfei commented 6 years ago

@ricmoo I test the case in node

const c = crypto.createCipheriv('aes-256-cfb', Buffer.from('4847afd2c068b1644d47709892bde97d', 'hex'), Buffer.from('a005f0cb74cb344427e545894bdec41a', 'hex');

I get the exception, why?

Error: Invalid key length
    at new Cipheriv (crypto.js:219:16)
    at Object.createCipheriv (crypto.js:619:10)
Ruffio commented 6 years ago

@aborigene has this been resolved and the issue be closed?

Ruffio commented 6 years ago

This issue should be closed due to lack of response...

Caleb-Irwin commented 4 years ago

Is this still a problem?