brix / crypto-js

JavaScript library of crypto standards.
Other
15.87k stars 2.39k forks source link

Wrong result with migration from Crypto.AES.encrypt into node:crypto #481

Open mkubasz opened 10 months ago

mkubasz commented 10 months ago

I'm trying to find some information with migration but hard to find some details.

I have two methods:

const hashEncryptNew = (i: string): string => {
    const CryptoKeyNew = createHash('md5').update(String('supertest').trim()).digest('hex');
    const iv = Buffer.alloc(16, 0);
    const cipher = createCipheriv('aes-256-cbc', CryptoKeyNew, iv);
    const encrypted = cipher.update(i, 'utf8', 'base64') + cipher.final('base64');
    return encrypted;
};

const hashEncrypt = (i: string): string => {
    const CryptoKey = MD5(String('supertest').trim()).toString();
    const str = CryptoAES.encrypt(i, CryptoKey).toString();
    return str;
};

console.log(hashEncrypt("dog is here")) // U2FsdGVkX19qSDsTe47nHhUgCfCCZerzdVHPu+vL+2s=
console.log(hashEncryptNew("dog is here")) // pJEo0zcGtqD/1cOu5b5K/g==

There is a problem that in old code has default iv. I tried to find similarities but the result has different length too. Probably I miss something but I will be glad for some hints.