Decrypt openssl AES with CryptoJS

Tagada

I'm trying to decrypt a file encrypted with openssl using CryptoJS 3.1.5.

Everything works fine if I encrypt and decrypt using CryptoJS, same goes for OpenSSL in shell, but when I try to mix CryptoJS with OpenSSL everything goes wrong.

The file is created using this command:

openssl enc -aes-256-cbc -in file.txt -out file.enc -k password

and I try to decrypt like this:

fs.readFile('file.enc', function(err, data) {
  var decrypted = CryptoJS.AES.decrypt(
                    data.toString(),
                    "password",
                    { mode : CryptoJS.mode.CBC }
                  );

  console.log(decrypted.toString(CryptoJS.enc.Utf8));
});

// Give me this err: Uncaught Error: Malformed UTF-8 data

And in the other way, I do :

fs.readFile('file.txt', function(err, data) {
  var encrypted = CryptoJS.AES.encrypt(
                    data.toString(),
                    "password",
                    { mode : CryptoJS.mode.CBC });

  fs.writeFile('file.enc', encrypted);
});

And then in Shell:

openssl enc -d -aes-256-cbc -in file.enc -out file2.txt -k password
// Give me this err: bad magic number

Am I missing something obvious ?

dave_thompson_085

Not definitely an answer yet but too much for comments:

Commandline openssl enc by default uses password-based encryption (PBE) with salt, which means the actual encryption key, and IV when applicable which it is for CBC, are computed from the given password and a random salt value by a Password Based Key Derivation Function that makes it more difficult for an adversary to try password-guessing attacks. I don't know your JS module (or much JS at all) but the webpage you link lists a variety of low-level primitives suggesting it does not automatically do PBE. A text string like "password" is (possibly) suitable for PBE, but not direct AES encryption where the key must be exactly 128, 192 or 256 bits and should be random binary data.

If you want openssl's semi-standard PBE, match it on the JS side; the item evpkey sounds possibly helpful, since EVP is the openssl module involved and I know no other (PB)KDF scheme that would be called EVP. If not, the enc default PBE is just MD5 of the password concatenated with the salt, iterated with feedback as many times as needed which in this case is three. See https://superuser.com/questions/455463/openssl-hash-function-for-generating-aes-key for an example in (mostly) perl. OpenSSL prefixes the 8 ASCII chars "Salted__" and the 8 bytes of salt to the file, so you need to remove those (and use the salt) before decrypt, or add them after encrypt.

If you want raw encryption, choose a more suitable key (on whichever side), and a unique and unpredictable IV unless you always use a new key in which case you can use a fixed IV, and on the openssl side use -K (note uppercase) and -iv to specify those values in hex. See the manpage on any Unix system with openssl installed or https://www.openssl.org/docs/manmaster/apps/enc.html .

Plus in either case enc defaults to "PKCS#5" (really PKCS#7) padding. I don't know if your JS module does; if not you should specify it. Unless you can guarantee your plaintexts will always be an exact multiple of 16 bytes (after any encoding like UTF8); then you could specify (or maybe default) no padding on the JS side and specify -nopad on the openssl side.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to decrypt with CryptoJS using AES?

From Dev

Unable to decrypt AES with OpenSSL

From Dev

Create CryptoJS AES Cipher Encryptor to Decrypt in .NET

From Dev

Create CryptoJS AES Cipher Encryptor to Decrypt in .NET

From Dev

Encrypt with AES using phpseclib and decrypt with CryptoJS

From Dev

CryptoJS and openssl_decrypt not produce same result

From Dev

sync AES ecnryption between cryptoJS and openSSL (ruby)

From Java

Encrypt file in java and decrypt in openssl with key aes

From Dev

Encrypt and decrypt string with c++, Openssl and aes

From Dev

Encrypt and decrypt string with c++, Openssl and aes

From Dev

CryptoJS encrypts AES with passphrase but PHP decrypt needs a key

From Dev

Decrypt AES/CBC/PKCS5Padding with CryptoJS

From Dev

How to decrypt a cryptojs AES encrypted message at the java server side?

From Dev

CryptoJS AES CBC 256 decrypt adds additional byte in the middle of plaintext

From Dev

Implement Php open_ssl_decrypt AES 256 CBC as CryptoJS

From Dev

How is an AES key processed when calling CryptoJS.AES.encrypt/decrypt with a non-standard key length?

From Dev

How to use openssl to decrypt data encrypted by Java using AES

From Dev

How can I encrypt / decrypt AES-256 CBC with OpenSSL?

From Dev

How to decrypt password from JavaScript CryptoJS.AES.encrypt(password, passphrase) in Python

From Dev

How to decrypt an encrypted AES-256 string from CryptoJS using Java?

From Dev

how to decrypt a file that was encrypted with CryptoJs3+ library with custom iterations and key sizes, in openssl

From Dev

Encrypt (cryptojs) - Decrypt (.NET)

From Dev

Encrypt with CryptoJS and decrypt with PHP

From Dev

decrypt using cryptojs not working

From Dev

Encrypt (cryptojs) - Decrypt (.NET)

From Dev

TPLB 3 OpenSSL Decrypt AES-256-CBC Encrypted with Ruby 2.0.0 OpenSSL::Cipher

From Dev

CryptoJS incorrect AES encoding

From Dev

cryptojs TDES convert to OPENSSL

From Dev

Can't decrypt string with CryptoJS

Related Related

  1. 1

    How to decrypt with CryptoJS using AES?

  2. 2

    Unable to decrypt AES with OpenSSL

  3. 3

    Create CryptoJS AES Cipher Encryptor to Decrypt in .NET

  4. 4

    Create CryptoJS AES Cipher Encryptor to Decrypt in .NET

  5. 5

    Encrypt with AES using phpseclib and decrypt with CryptoJS

  6. 6

    CryptoJS and openssl_decrypt not produce same result

  7. 7

    sync AES ecnryption between cryptoJS and openSSL (ruby)

  8. 8

    Encrypt file in java and decrypt in openssl with key aes

  9. 9

    Encrypt and decrypt string with c++, Openssl and aes

  10. 10

    Encrypt and decrypt string with c++, Openssl and aes

  11. 11

    CryptoJS encrypts AES with passphrase but PHP decrypt needs a key

  12. 12

    Decrypt AES/CBC/PKCS5Padding with CryptoJS

  13. 13

    How to decrypt a cryptojs AES encrypted message at the java server side?

  14. 14

    CryptoJS AES CBC 256 decrypt adds additional byte in the middle of plaintext

  15. 15

    Implement Php open_ssl_decrypt AES 256 CBC as CryptoJS

  16. 16

    How is an AES key processed when calling CryptoJS.AES.encrypt/decrypt with a non-standard key length?

  17. 17

    How to use openssl to decrypt data encrypted by Java using AES

  18. 18

    How can I encrypt / decrypt AES-256 CBC with OpenSSL?

  19. 19

    How to decrypt password from JavaScript CryptoJS.AES.encrypt(password, passphrase) in Python

  20. 20

    How to decrypt an encrypted AES-256 string from CryptoJS using Java?

  21. 21

    how to decrypt a file that was encrypted with CryptoJs3+ library with custom iterations and key sizes, in openssl

  22. 22

    Encrypt (cryptojs) - Decrypt (.NET)

  23. 23

    Encrypt with CryptoJS and decrypt with PHP

  24. 24

    decrypt using cryptojs not working

  25. 25

    Encrypt (cryptojs) - Decrypt (.NET)

  26. 26

    TPLB 3 OpenSSL Decrypt AES-256-CBC Encrypted with Ruby 2.0.0 OpenSSL::Cipher

  27. 27

    CryptoJS incorrect AES encoding

  28. 28

    cryptojs TDES convert to OPENSSL

  29. 29

    Can't decrypt string with CryptoJS

HotTag

Archive