AES decryption with cipher using CryptoJS returns a blank value


stubborn

situation

I have the following code:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<div id="decrypted">Please wait...</div>
Insert new note:<input type="text" id="new_note"><input type="button" id="enc_button" value="Save">
<script>
    var password = "testpassword";
    var encrypted_text = localStorage.getItem("encrypted");
    var rawData = atob(encrypted_text);
    var iv = rawData.substring(0,16);
    var crypttext = rawData.substring(16);
    var plaintextArray = CryptoJS.AES.decrypt(
      { ciphertext: CryptoJS.enc.Latin1.parse(crypttext) },
      CryptoJS.enc.Hex.parse(password),
      { iv: CryptoJS.enc.Latin1.parse(iv) }
    );
    var decrypted = CryptoJS.enc.Latin1.stringify(plaintextArray);
    document.getElementById("decrypted").innerHTML = decrypted;
    document.getElementById("enc_button").onclick = function(){
    	var text = document.getElementById("new_note").value;
    	var encrypted = CryptoJS.AES.encrypt(text, password);
    	localStorage.setItem("encrypted",encrypted);
    }
</script>

what should my code do

Encrypt string with AES using CryptoJS; decrypt encrypted text saved in local storage and display result in div

what doesn't work

Although the string appears to be encrypted, the variable decryptis empty. The chrome console doesn't trigger any errors.

my question

How to successfully encrypt and decrypt text?

Artjom B.

CryptoJS has two slightly different encryption/decryption types.

when using it

var encrypted = CryptoJS.AES.encrypt(text, password);

then you are using password based encryption which is not the same as plain key/IV based encryption. This means that the password and randomly generated salt will be run through a single MD5 call to generate the key and IV for the actual encryption. This is OpenSSL compatible encryption. This encryptedobject stores the random salt used to generate the key and IV.

When you encryptedcast to a string (e.g. add it to localStorage) it will be converted to an OpenSSL compatible string encoding that includes the salt. In order to decrypt it again, you don't need to mess with the IV or salt, as CryptoJS does this for you automatically:

var decrypted = CryptoJS.AES.decrypt(encrypted, password);

Remember, this decryptedis an WordArrayobject, and when you force it to a string, it encodes the content to hex by default. If you don't want to do this, you need to specify the encoding yourself, e.g. UTF-8.

A blank value is usually returned when decryption fails for some reason (such as wrong key, wrong ciphertext, or wrong encoding). CryptoJS doesn't throw custom error messages, but will try to continue since you should know what you're doing.

Full code:

var password = "testpassword";

document.getElementById("enc_button").onclick = function(){
  var text = document.getElementById("new_note").value;
  
  var encrypted = CryptoJS.AES.encrypt(text, password);
  encrypted = encrypted.toString();
  
  var decrypted = CryptoJS.AES.decrypt(encrypted, password);
  decrypted = decrypted.toString(CryptoJS.enc.Utf8)
  
  document.getElementById("decrypted").innerHTML = decrypted;
}
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/aes.js"></script>
<div id="decrypted">Please wait...</div>
Insert new note:<input type="text" id="new_note"><input type="button" id="enc_button" value="Encrypt & Decrypt">

Related


AES decryption with cipher using CryptoJS returns a blank value

stubborn situation I have the following code: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <div id="decrypted">Please wait...</div> Insert new note:<input type="text" id="new_note"><input type="button" id="enc_but

AES decryption with cipher using CryptoJS returns a blank value

stubborn situation I have the following code: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <div id="decrypted">Please wait...</div> Insert new note:<input type="text" id="new_note"><input type="button" id="enc_but

Decryption with AES returns blank string

sruly: import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.*; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.util.Arrays; import java

Decryption with AES returns blank string

sruly: import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.*; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.util.Arrays; import java

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared key. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it in

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

AES encryption/decryption in javascript using CryptoJS

Anthracene I am trying to send AES encrypted messages between javascript and php using a shared secret. In Javascript, I am using the CryptoJS library. In php, I am using mycrypt. I am trying to construct an encrypted message in javascript and then decrypt it

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

CryptoJS AES 256 ECB decryption

Marcus G. I have tried aes-ecb-js and now can try cryptoJS to solve my problem. I've read some topics and googled a lot, but I can't decrypt the HEX string using AES ECB 256. When using an online decoder, it works fine: I tried the following code according to

AES encryption with CryptoJS and decryption with CodeIgniter

fire brand I'm trying to encrypt a username (written in Codeigniter 3) sent to my server via a POST request, so I'm using CryptoJS to do it client-side like this: var user = $('.user').val(); var key = "<? echo($key);?>"; //$key is created on the server side v

CryptoJS encryption in AES-256-CBC returns unexpected value

Alex Bryce I'm encrypting some data using CryptoJS and comparing it with online tools , but the result is not the same. In fact, the results of CryptoJS cannot be decrypted with the tool. I am trying to encrypt in AES-256-CBC with the following parameters: tex

CryptoJS encryption in AES-256-CBC returns unexpected value

Alex Bryce I'm encrypting some data using CryptoJS and comparing it with online tools , but the result is not the same. In fact, the results of CryptoJS cannot be decrypted with the tool. I am trying to encrypt in AES-256-CBC with the following parameters: tex

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

CryptoJS AES256 decryption problem

Toharawk I'm having some problems decrypting an AES256 encoded URL. For the decryption process using CryptoJS, but the output throws the following exception: Malformed UTF-8 data What did I miss? code: router.post("/signin/:eoLogin2", async (req, res) => {

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b

AES CBC: JavaScript/CryptoJS encryption -> Golang decryption

DjH Note: This is for personal use and learning only, I don't plan to use my encryption for public use. I need to AES256 encrypt the string, but my current attempt ends up with something like the server side string Salted__Vέ��|��l��ʼ8XCQlYwhen hex decoded . W

AES-CTR encryption in Go and decryption in CryptoJS

Mark: I'm having trouble decrypting text encrypted in Go lang using CryptoJS. Here is the Go code : https://play.golang.org/p/xCbl48T_iN package main import ( "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) func main() { key := []b