How to decrypt with CryptoJS using AES?


User 3197788

With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right.

I am encrypting like this:

  var ENC_KEY = "bXlrZXk=";  //"mykey"

  var encrypted = CryptoJS.AES.encrypt("hello",  CryptoJS.enc.Base64.parse(ENC_KEY), 
    {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
  console.log('encrypted: ' + encrypted);

It works as expected and outputs the encrypted value I expect, but when I decrypt it using the method below, I end up with an empty object:

var decrypted = CryptoJS.AES.decrypt(encrypted, CryptoJS.enc.Base64.parse(ENC_KEY), 
    {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    console.log('decrypted: ' + decrypted);

I have also tried using:

console.log('encrypted is decrypted to: ' + decrypted.toString(CryptoJS.enc.Utf8);

but no joy...

Neil Damon

I tried it in a fiddle (with some modifications to make it work):

//decrypt gives a hex
function hex2a(hexx) {
    var hex = hexx.toString();//force conversion
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}

var key = CryptoJS.enc.Base64.parse("Secret Passphrase"); 

alert(key);

var encrypted = CryptoJS.AES.encrypt("hello",  key, 
    {
        mode: CryptoJS.mode.ECB
    });

alert('encrypted: ' + encrypted);

var decrypted = CryptoJS.AES.decrypt(encrypted, key, 
    {
        mode: CryptoJS.mode.ECB
    });

alert('decrypted: ' + hex2a(decrypted));

http://jsfiddle.net/gttL705r/

and found that the decryption returned a hex (probably not a string)...could that be causing your problem? So with the quick hex2ascii function, 'hello' is returned :)

I also removed the specified padding because Pkcs7 is said to be the default in the documentation, but I can't find the src js I need to download.

Related


How to decrypt cryptojs AES encrypted message on Java server side?

user1455719: I have the following cryptojs based javascript encrypt/decrypt functions and they work fine. When encrypting a message with cryptpjs, I use a random salt, a random iv value and a specific cipher. When decrypting the encrypted message, I reused the

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. look like: public static String encrypt(byte[] key, byte[] initVector, String valu

How to decrypt cryptojs AES encrypted message on Java server side?

user1455719: I have the following cryptojs based javascript encrypt/decrypt functions and they work fine. When encrypting a message with cryptpjs, I use a random salt, a random iv value and a specific cipher. When decrypting the encrypted message, I reused the

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

Kent I have an encrypted AES-256 string from CryptoJS with a passphrase. I need to decrypt it in Java but can't figure out how to do it. It seems that IV, key and salt are required to decrypt, as in the CryptoJS homepage , the encrypted data already contains a

Create a CryptoJS AES password encryptor to decrypt in .NET

Jamie Barker I'm trying to create encryption/decryption that will work between server/client. I use CryptoJS on the client side and ASP.Net (VB.Net) on the server side. The server side is used in many places so it can't be changed, so CryptoJS needs to work ar

Unable to decrypt password using CryptoJS

Jojo 01 I am trying to encrypt using CryptoJS. The encryption seems to work fine, but when I decrypt the password, I don't get the initial plain text password. Here is my javascript code: <script type="text/javascript"> var password = ""; function noSsl() {

Create a CryptoJS AES password encryptor to decrypt in .NET

Jamie Barker I'm trying to create encryption/decryption that will work between server/client. I use CryptoJS on the client side and ASP.Net (VB.Net) on the server side. The server side is used in many places so it can't be changed, so CryptoJS needs to work ar

How to decrypt AES using PHP?

User 4027821 I am using this library to create an AES encrypted string on Android and add this string to MySQL. Here are the parameters for lib: Now I want to decrypt the string via PHP: Here is the encrypted string: Plaintext: a z8OVzJvtKHPCdT6PeFoEww==(encry

How to Decrypt AES Ciphertext Using NCryptDecrypt on Windows

Seb I'm trying to encrypt plain text with AES using Ncrypt.lib and then decrypt it. I use Ncrypt.lib because I want to use a persistent symmetric key. My problem is that the decryption part works. In fact, I'm not decrypting my first 16 bytes correctly. #inclu

Decrypt openssl AES with CryptoJS

supply I am trying to decrypt a file encrypted with openssl using CryptoJS 3.1.5 . If I use CryptoJS for encryption and decryption, everything works fine, as does OpenSSL in the shell, but when I try to mix CryptoJS with OpenSSL everything goes wrong. Create t

Encrypt with AES with phpseclib and decrypt with CryptoJS

Roberto 14 I'm trying to encrypt a string using phpseclib AES in CBC mode (the library's default): $cipher = new Crypt_AES(); $cipher->setKey('abcdefghijklmnop'); $cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3)); $cipher->encrypt("hello worl

Unable to decrypt string using CryptoJS

Alvaro Maceda I'm trying to encode/decode data using CryptoJS as a preliminary test of the code I'm developing. Here is the code I use for encryption: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var mess

Unable to decrypt string using CryptoJS

Alvaro Maceda I'm trying to encode/decode data using CryptoJS as a preliminary test of the code I'm developing. Here is the code I use for encryption: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var mess

Create a CryptoJS AES password encryptor to decrypt in .NET

Jamie Barker I'm trying to create encryption/decryption that will work between server/client. I use CryptoJS on the client side and ASP.Net (VB.Net) on the server side. The server side is used in many places so it can't be changed, so CryptoJS needs to work ar

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

Kent I have an encrypted AES-256 string from CryptoJS with a passphrase. I need to decrypt it in Java but can't figure out how to do it. It seems that IV, key and salt are required to decrypt, as in the CryptoJS homepage , the encrypted data already contains a

Create a CryptoJS AES password encryptor to decrypt in .NET

Jamie Barker I'm trying to create encryption/decryption that will work between server/client. I use CryptoJS on the client side and ASP.Net (VB.Net) on the server side. The server side is used in many places so it can't be changed, so CryptoJS needs to work ar

How to decrypt cryptojs AES encrypted message on Java server side?

user1455719: I have the following cryptojs based javascript encrypt/decrypt functions and they work fine. When encrypting a message with cryptpjs, I use a random salt, a random iv value and a specific cipher. When decrypting the encrypted message, I reused the

Unable to decrypt password using CryptoJS

Jojo 01 I am trying to encrypt using CryptoJS. The encryption seems to work fine, but when I decrypt the password, I don't get the initial plain text password. Here is my javascript code: <script type="text/javascript"> var password = ""; function noSsl() {

Decrypt openssl AES with CryptoJS

supply I am trying to decrypt a file encrypted with openssl using CryptoJS 3.1.5 . If I use CryptoJS for encryption and decryption, everything works fine, as does OpenSSL in the shell, but when I try to mix CryptoJS with OpenSSL everything goes wrong. Create t

AES encryption using CryptoJS

Sergio David Romero I need to implement AES encryption using JavaScript. Use AES/CBC/NoPadding mode, and created a way to do it in 16-length blocks. I have solved it using Java. looks like: public static String encrypt(byte[] key, byte[] initVector, String val

Encrypt with AES with phpseclib and decrypt with CryptoJS

Roberto 14 I'm trying to encrypt a string using phpseclib AES in CBC mode (the library's default): $cipher = new Crypt_AES(); $cipher->setKey('abcdefghijklmnop'); $cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3)); $cipher->encrypt("hello worl

Unable to decrypt string using CryptoJS

Alvaro Maceda I'm trying to encode/decode data using CryptoJS as a preliminary test of the code I'm developing. Here is the code I use for encryption: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var mess

How to decrypt with CryptoJS using AES?

User 3197788 With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right. I am encrypting like this: var ENC_KEY = "bXlrZXk="; //"mykey" var encrypted = CryptoJS.AES.encrypt("hello", Crypto

Create a CryptoJS AES password encryptor to decrypt in .NET

Jamie Barker I'm trying to create encryption/decryption that will work between server/client. I use CryptoJS on the client side and ASP.Net (VB.Net) on the server side. The server side is used in many places so it can't be changed, so CryptoJS needs to work ar

Unable to decrypt password using CryptoJS

Jojo 01 I am trying to encrypt using CryptoJS. The encryption seems to work fine, but when I decrypt the password, I don't get the initial plain text password. Here is my javascript code: <script type="text/javascript"> var password = ""; function noSsl() {