Maybe you followed my series on symmetric encryption with AES in PHP. You can check it out here if you’re interested! What if I told you, that you don’t even need a server to do that but might as well encrypt text using AES right in your browser? Sounds crazy? Well, you’re in for a treat!

Dependencies

I can’t stress this enough, so here I go again: when it comes to cryptography, there are two main rules:

  • Stick to standards wherever possible!

  • Don’t do it unless you know what you’re doing!

I really don’t feel qualified to implement AES in JavaScript on my own. Luckily, there’s a project called ‘CryptoJS’ that calls itself the “JavaScript library of crypto standards” – so exactly what we’re looking for!

You can download the project here. There really are plenty of standards included in the library, such as MD5, HMAC and the entire SHA-family. We don’t need all those files though. To keep our project clean, we just include the ones we actually use. These are:

  • src/aes.js

  • src/pbkdf2.js

  • src/sha1.js

  • src/sha256.js

We’re going to build a helper class for using the provided functionality a little more conveniently.

One important thing about CryptoJS: it works with ‘Words’ or ‘WordArrays’ in some places. Words are 32bit long, calling for conversion here and there. So everytime you see a ’32’ in the code, it’s because of that.

The wrapper

What we’ll end up with is a JavaScript ‘class’ that can be used pretty much independently in all your JavaScript-projects.

Here’s the base for this project:

Copy to Clipboard

Just like in the PHP-version we’re defining the size of blocks and the key as well as the iteration count. I chose the same values as we did for PHP to have the output transferable inbetween platforms. Note that blockSize and keySize are set in Bits, not Bytes as we did in PHP!

The code

Let’s start with encryption. I tried to keep the structure of the code similar to the PHP version to make it easier to follow. I’m not going to cover all the details of what’s happening – you can read about that here.

Copy to Clipboard

CryptoJS provides its own method to get random data for our IV and salt. Unfortunately, it takes its values in bytes, so we convert.

Generating a key

The code for generating a key is a bit bulky and we’re going to need it again when decrypting, so I put it in a seperate function to keep things clean:

Copy to Clipboard

Using PBKDF2 with SHA1 as hashing algorithm to generate the key. Be aware that the keySize is expected in ‘Words’!

Encrypting

CryptoJS’ encrypt() method also requires quite some parameters. Note that we need to explicitly name what padding we want to be applied (unlike in PHP where PKCS5 was used by default). Also note that PKCS5 and PKCS7 are interchangeable, so encrypting with one and decrypting with the other works just fine.

The returned ciphertext will be an array of Words.

Encoding

To be able to safely transfer the encrypted ciphertext around the web, we’re going to encode it using a modified version of Base64. But since IV, salt and ciphertext are WordArrays, we need to convert them first.

This is easy regarding the ciphertext as it comes with a built-in ‘toString()’ function that returns the Base64-encoded version of it. For IV and salt we first have to convert them to Base64 by hand.

To get raw data from all of the three, we decode them using atob(). This raw data we pass to our base64UrlEncode:

Copy to Clipboard

This replaces ‘+’ with ‘-‘ and ‘/’ with ‘_’ to avoid problems in URLs.

Signing

Signing our resulting encoded IV, salt and ciphertext for integrity is easy in CryptoJS:

Copy to Clipboard

This gives us a SHA256-HMAC for our Base64-string that we append to it separated by a ‘:’ for easy extraction later.

Usage

Using this library is very straightforward. Simply link all JavaScript files, call Crypto.encrypt() and you’re done! Here’s an example markup:

Copy to Clipboard

Conclusion

And that’s it! We now have a convenient way of encrypting strings in JavaScript. To decrypt them, you can either use the class we built in PHP, or check out the [next post](link)!