Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CryptoJS AES encryption/decryption JavaScript and command line examples

You can run these commands to encrypt or decrypt a string:

Command

To encrypt:

printf "Lorem ipsum dolor sit amet, ..." | \
  openssl enc -e -base64 -A -aes-256-cbc -pass pass:"my-password"

To decrypt:

printf "U2FsdGVkX1/l/LqNSCQixd0iPv4neKAGZvbQDbYUovZE4OcM7l3ULNDgkZQmrweN" | \
  openssl enc -d -base64 -A -aes-256-cbc -pass pass:"my-password"

Note: Other users can see your text content and password in commands above.

Interactive

To encrypt:

echo Text to encrypt:; read STRING; \
printf "Password: "; read -s PASS; \
echo; echo Encrypted:; printf "$STRING" | \
openssl enc -e -base64 -A -aes-256-cbc -pass pass:"$PASS"

To decrypt:

echo Encrypted text:; read STRING; \
printf "Password: "; read -s PASS; \
echo; echo Decrypted:; printf "$STRING" | \
openssl enc -d -base64 -A -aes-256-cbc -pass pass:"$PASS"

Browser

You can open console of your browser, copy and run these commands to use CryptoJS:

var s=document.createElement('script');
s.src='http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js';
document.body.appendChild(s);

You can use CryptoJS AES once the script is loaded:

CryptoJS.AES.encrypt("Lorem ipsum dolor sit amet, ...", "my-password").toString();
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
var string = 'Lorem ipsum dolor sit amet, ...';
var password = 'my-password';
var encrypted = CryptoJS.AES.encrypt(string, password);
console.log(encrypted.toString());
// will output something like:
// U2FsdGVkX1/l/LqNSCQixd0iPv4neKAGZvbQDbYUovZE4OcM7l3ULNDgkZQmrweN
var decrypted = CryptoJS.AES.decrypt(encrypted, password);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
// Lorem ipsum dolor sit amet, ...
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.