Encryption made Simple with CryptoJS
By: Ben Yoss
Tags: JavaScript, Cybersecurity, Passwords, Nodejs, Cipher

Encryption in the JavaScript environment is the primary focus of keeping private information secure through binary algorithms. Through the process of scrambling strings and integers, these algorithms follow a most suitable result to protect against hackers.

However, developers tend to not self-encrypt private information in their web applications due to the tediousness and insecurities that follow it. Instead most tend to turn towards a library which does the work for them in a more experienced manner, but the problem with these libraries is that they continue to complicate the practice. This is when simplification took a step further and Crypto-JS was born.

Crypto-JS is a cryptographic library meant to enforce algorithms followed by best practice means condensed into the simplified use of classes/method calls.

Today I am going to present a demo showcasing the features Crypto-JS can bring to secure your next web application. Let’s get started.

Encrypting a string:

What we will be covering first is how to utilize basic functions from Crypto-JS to create a encrypted array of characters. We can accomplish this task by first demonstrating how to encrypt a basic string:

A basic hash function such as the variants listed above will normally require a toString method attached to the back in order to see the stringed hash created. Typically these strings will look similar to this:

Raw: {[Array]}

Stringified: 78e731027d8fd50ed642340b7c9a63b3

This hash function can still be encrypted even further. By applying a additional method known as enc, we can encrypt the hash into any format we desire. For the sake of this presentation, I will demonstrate using Base64:

Creating Ciphers:

Before we jump straight into decrypting, there is one important detail that is needed to be discussed. Hashes CANNOT be decrypted once created. In order to decrypt a hash, they must first be defined with a secret by using a cipher. Ciphers are best used when dealing with encrypted hashes that need to be compared with another hash.

Now if we wanted to, we could add additional options such as salt to further encrypt our ciphertext for extra security, or deal with progressive hashing, which combines the results of 2 or more hashes to create a ciphertext. But for the sake of this blog, we are keeping it simple.

Decrypting a Encrypted Cipher:

Now that we have covered how to create a basic hash and encrypt that hash using ciphers, let’s talk about the vise-versa; Decryption. Decryption, as stated earlier, can only be applied to ciphers since ciphers are persisted and can e called with the secret string defining them. Let’s decrypt the ciphertext we have created earlier:

And boom! The cipher that was created earlier was retrieved based on the secret assigned is decrypted back to its original string form. Now if we wanted to we could decrypted our ciphertext into any format, such as base64, ASCII, or even binary to name a few.

Conclusion:

Through the course of reading this article, we have covered exactly what Crypto-JS does as a library and touched on how to create hashes in the four various forms. Additionally, we have demonstrated how to encrypt strings using ciphers and decrypting ciphertext back into a string. Hopefully this article has shed some light on the world of encryption and that you have left this article learning more than entering.