Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert to string and back again with CryptoJs

var encrypted = CryptoJS.AES.encrypt(jsonStr, 'youngunicornsrunfree', { format: JsonFormatter });

//convert encrypted to a string for transfer
//convert string back to Crypto object so it can be decrypted.

var decrypted = CryptoJS.AES.decrypt(encrypted, "youngunicornsrunfree", { format: JsonFormatter });

The above two steps, work fine. But in between I need to convert encrypted to a string for transmitting over a network and then convert it back. How can I do this?

like image 409
williamsandonz Avatar asked Jan 22 '14 18:01

williamsandonz


People also ask

What is IV in CryptoJS?

In case of AES-256 - block size is, obviously, 256 bits, which is 32 bytes, which is exactly what you get by CryptoJS. enc. Base64. parse() of 22 byte Base64 string. According to specification and algorithm, IV is exactly block size length, which is 32 bytes with AES-256.

What is secret passphrase in CryptoJS?

It means that the key (secret passphrase) used to encrypt and decrypt is the same. So, it's security relies mostly in keeping the key secure. If someone obtains the key, they will be able to decrypt anything encrypted with that passphrase.

What is the use of CryptoJS?

CryptoJS is a growing collection of standard and secure cryptographic algorithms implemented in JavaScript using best practices and patterns. They are fast, and they have a consistent and simple interface.

What's the cryptojs_bytearraywordarrayconversions file?

Raw CryptoJS_byteArrayWordArrayConversions.js This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

How do I send an encrypted string to the server?

We can do this easily by pulling out the encrypted string with toString (). Sounds to simple right? Say we need to send the encrypted jsonStr to the server.

How to convert a value to a string in JavaScript?

How to convert a value to a string in JavaScript? There are 5 ways to convert a value to a string. They are Concatenating empty strings In the following example, all the above-mentioned methods were used to convert a value to a string and the final result was displayed as shown in the output.


1 Answers

Let's simplify this to be able to get to the problem. Firs we start with something like this:

jsonStr = '{"something":"else"}';
var encrypted = CryptoJS.AES.encrypt(jsonStr, 'youngunicornsrunfree');
var decrypted = CryptoJS.AES.decrypt(encrypted, "youngunicornsrunfree");
console.log(decrypted.toString(CryptoJS.enc.Utf8));

This gives us our answer jsonStr after we encrypt it then decrypt it. But say we want to send it to the server. We can do this easily by pulling out the encrypted string with toString(). Sounds to simple right? Say we need to send the encrypted jsonStr to the server. Try this

jsonStr = '{"something":"else"}';
var encrypted = CryptoJS.AES.encrypt(jsonStr, 'youngunicornsrunfree');
console.log("We send this: "+encrypted.toString());

Now say we sent something earlier and we are getting it back. We can do something like this:

var messageFromServer = "U2FsdGVkX19kyHo1s8+EwNuo/LQdL3RnSoDHU2ovA88RtyOs+PvpQ1UZssMNfflTemaMAwHDbnWagA8lQki5kQ==";
var decrypted = CryptoJS.AES.decrypt(messageFromServer, "youngunicornsrunfree");
console.log(decrypted.toString(CryptoJS.enc.Utf8));
like image 66
DutGRIFF Avatar answered Oct 21 '22 23:10

DutGRIFF