Stubborn
Stubborn

Reputation: 968

Encrypt/decrypt with two different keys

My code

I'm encrypting a string with two different keys with CryptoJS:

var password = "testpassword";
var serverkey = "randomkey";
var text = document.getElementById("new_note").value;
var encrypted1 = CryptoJS.AES.encrypt(text, password);
encrypted1 = encrypted1.toString();
var encrypted = CryptoJS.AES.encrypt(encrypted1,serverkey);

And trying to decrypt it with this code:

var password = "testpassword";
var serverkey = "randomkey";
var encrypted_text = localStorage.getItem("encrypted");
var decrypted1 = CryptoJS.AES.decrypt(encrypted_text,serverkey);
decrypted1 = decrypted.toString();
var decrypted = CryptoJS.AES.decrypt(decrypted1,password);
decrypted = decrypted.toString(CryptoJS.enc.Utf8);
document.getElementById("decrypted").innerHTML = decrypted;

What isn't working

While the encryption seems to work fine, when I try to convert decrypted1 to a string in order to decrypt it the second time, I get Cannot read property 'toString' of undefined on the chrome console. This should mean that the first decryption process returns an empty string.

My question

How can I fix this problem?

Upvotes: 1

Views: 1396

Answers (2)

Artjom B.
Artjom B.

Reputation: 61952

There is a typo in your variable names. Check where you define decrypted and where you use it. You meant to use decrypted1.

Additionally, you have a problem with the encoding. The first decrypted1.toString(); will encode the string into Hex, but earlier you called encrypted1.toString(); which does not encode to Hex, but a special Base64 encoding (OpenSSL compatible). You will need to encode to UTF-8 in order to get to the same encoding that you had before during encryption.

Here is the working code:

document.getElementById("enc_button").onclick = function(){
  var password = "testpassword";
  var serverkey = "randomkey";
  var text = document.getElementById("new_note").value;
  var encrypted1 = CryptoJS.AES.encrypt(text, password);
  encrypted1 = encrypted1.toString();
  var encrypted = CryptoJS.AES.encrypt(encrypted1, serverkey);

  var decrypted1 = CryptoJS.AES.decrypt(encrypted,serverkey);
  decrypted1 = decrypted1.toString(CryptoJS.enc.Utf8);
  var decrypted = CryptoJS.AES.decrypt(decrypted1,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>
<div>
  Insert new note:
  <input type="text" id="new_note">
  <input type="button" id="enc_button" value="Encrypt & Decrypt">
</div>

Upvotes: 3

Julio Soares
Julio Soares

Reputation: 1190

It looks like decripted IS empty. you have initialized decripted1 var decrypted1 = CryptoJS.AES.decrypt(encrypted_text,serverkey); and then tryed to "toString()" the uninitialized decrypted var decrypted1 = decrypted.toString(); Although I think you don't need this line... (?).

Upvotes: 0

Related Questions