brix / crypto-js

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

Content encrypted uisng openssl cannot be decrypted using crypto-js #375

Closed Alanscut closed 3 years ago

Alanscut commented 3 years ago

Using openssl cli encrypted the string 'hahahahahahahahahahahahahahahaha'

printf "hahahahahahahahahahahahahahahaha" | /usr/bin/openssl enc  -aes-256-cbc -pass pass:"pass" -S "cc00000000000000" -e -base64 -p -nopad

result:

salt=CC00000000000000
key=CD089A8F7F4F938437BF126233D76F59A6B1E4C3BCEE2B1A8819229985545A63
iv =525274989F61DDA0F71979E5140E2565
U2FsdGVkX1/MAAAAAAAAANcQ3NNYy8zIC9jzcAHAwxNYIM6/88Xdy/eMhRD4kCxh

Using crypto-js decrypt the ciphertext 'U2FsdGVkX1/MAAAAAAAAANcQ3NNYy8zIC9jzcAHAwxNYIM6/88Xdy/eMhRD4kCxh'

var CryptoJS = require('crypto-js');

const decrypted = CryptoJS.AES.decrypt(
    'U2FsdGVkX1/MAAAAAAAAANcQ3NNYy8zIC9jzcAHAwxNYIM6/88Xdy/eMhRD4kCxh', 
    'pass', 
    {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.NoPadding,
        blockSize: 256/32
    }
)
console.log(decrypted.toString(CryptoJS.enc.Utf8));

expected: hahahahahahahahahahahahahahahaha but an error was reported: Error: Malformed UTF-8 data

crypto-js decrypts the cipertext in different ways from openssl. Does the crypto-js intend to provide support for decrypting the cipertext encrypted in openssl? @evanvosberg

Alanscut commented 3 years ago

It took me a long time to understand that crypto-js uses MD5 to generate keys, whereas OpenSSL uses SHA256 to generate keys by default. Therefore, the OpenSSL encryption command should be as follows: printf "hahahahahahahahahahahahahahahaha" | /usr/bin/openssl enc -aes-256-cbc -pass pass:"pass" -S "cc00000000000000" -e -base64 -p -nopad -md md5 result:

salt=CC00000000000000
key=A6E7A6052892AC89CF80A4AE58A74C7F7C65D54CB03812DC21AFF64F6BF23D19
iv =372D34788469EA8B2F7F9A3400AA65AA
U2FsdGVkX1/MAAAAAAAAAPqHYc4dS/BfUX3vlVvkdNhbVuW915Oz8W9dqjXv0ldA

Using crypto-js decrypt the ciphertext 'U2FsdGVkX1/MAAAAAAAAAPqHYc4dS/BfUX3vlVvkdNhbVuW915Oz8W9dqjXv0ldA' var CryptoJS = require('crypto-js');

const decrypted = CryptoJS.AES.decrypt(
    'U2FsdGVkX1/MAAAAAAAAAPqHYc4dS/BfUX3vlVvkdNhbVuW915Oz8W9dqjXv0ldA', 
    'pass', 
    {
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.NoPadding,
        blockSize: 256/32
    }
)
console.log(decrypted.toString(CryptoJS.enc.Utf8));

It works fine, get the string hahahahahahahahahahahahahahahaha