Javascript AES encryption

JavascriptEncryptionAes

Javascript Problem Overview


Is there a library available for AES 256-bits encryption in Javascript?

Javascript Solutions


Solution 1 - Javascript

JSAES is a powerful implementation of AES in JavaScript. http://point-at-infinity.org/jsaes/

Solution 2 - Javascript

Here's a demonstration page that uses slowAES.

slowAES was easy to use. Logically designed. Reasonable OO packaging. Supports knobs and levers like IV and Encryption mode. Good compatibility with .NET/C#. The name is tongue-in-cheek; it's called "slow AES" because it's not implemented in C++. But in my tests it was not impractically slow.

It lacks an ECB mode. Also lacks a CTR mode, although you could build one pretty easily given an ECB mode, I guess.

It is solely focused on encryption. A nice complementary class that does RFC2898-compliant password-based key derivation, in Javascript, is available from Anandam. This pair of libraries works well with the analogous .NET classes. Good interop. Though, in contrast to SlowAES, the Javascript PBKDF2 is noticeably slower than the Rfc2898DeriveBytes class when generating keys.

It's not surprising that technically there is good interop, but the key point for me was the model adopted by SlowAES is familiar and easy to use. I found some of the other Javascript libraries for AES to be hard to understand and use. For example, in some of them I couldn't find the place to set the IV, or the mode (CBC, ECB, etc). Things were not where I expected them to be. SlowAES was not like that. The properties were right where I expected them to be. It was easy for me to pick up, having been familiar with the Java and .NET crypto programming models.

Anandam's PBKDF2 was not quite on that level. It supported only a single call to DeriveBytes function, so if you need to derive both a key and an IV from a password, this library won't work, unchanged. Some slight modification, and it is working just fine for that purpose.

EDIT: I put together an example of packaging SlowAES and a modified version of Anandam's PBKDF2 into Windows Script Components. Using this AES with a password-derived key shows good interop with the .NET RijndaelManaged class.

EDIT2: the demo page shows how to use this AES encryption from a web page. Using the same inputs (iv, key, mode, etc) supported in .NET gives you good interop with the .NET Rijndael class. You can do a "view source" to get the javascript for that page.

EDIT3
a late addition: Javascript Cryptography considered harmful. Worth the read.

Solution 3 - Javascript

In my searches for AES encryption i found this from some Standford students. Claims to be fastest out there. Supports CCM, OCB, GCM and Block encryption. http://crypto.stanford.edu/sjcl/

Solution 4 - Javascript

Googling "JavaScript AES" has found several examples. The first one that popped up is designed to explain the algorithm as well as provide a solution:

Movable Type Scripts: AES

Solution 5 - Javascript

This post is now old, but the crypto-js, may be now the most complete javascript encryption library.

CryptoJS is a collection of cryptographic algorithms implemented in JavaScript. It includes the following cyphers: AES-128, AES-192, AES-256, DES, Triple DES, Rabbit, RC4, RC4Drop and hashers: MD5, RIPEMD-160, SHA-1, SHA-256, SHA-512, SHA-3 with 224, 256, 384, or 512 bits.

You may want to look at their Quick-start Guide which is also the reference for the following node.js port.

node-cryptojs-aes is a node.js port of crypto-js

Solution 6 - Javascript

Recently I had the need to perform some encryption/decryption interoperability between javascript and python.

Specifically...

  1. Using AES to encrypt in javascript and decrypt in python (Google App Engine)
  2. Using RSA to encrypt in javascript and decrypt in python (Google App Engine)
  3. Using pycrypto

I found lots and lots of different versions of RSA and AES floating around the web and they were all different in their approach but I did not find a good example of end to end javascript and python interoperability.

Eventually I managed to cobble together something that suited my needs after a lot of trial and error.

Anyhow I knocked up an example of a js/webapp talking to a google app engine hosted python server that uses AES and public key and private key RSA stuff.

I though I'd include it here by link in case it will be of some use to others who need to accomplish the same thing.

http://www.ipowow.com/files/aesrsademo.tar.gz

and see demo at rsa-aes-demo DOT appspot DOT com

edit: look at the browser console output and also view source to get some hints and useful messages as to what's going on in the demo

edit: updated very old and defunct link to source to now point to

https://sestertii.com/files/aesrsademo.tar.gz

Solution 7 - Javascript

Judging from my own experience, asmcrypto.js provides the fastest AES implementation in JavaScript (especially in Firefox since it can fully leverage asm.js there).

From the readme:

> Chrome/31.0 > SHA256: 51 MiB/s (9 times faster than SJCL and CryptoJS) > AES-CBC: 47 MiB/s (13 times faster than CryptoJS and 20 times faster than SJCL) > > Firefox/26.0 > SHA256: 144 MiB/s (5 times faster than CryptoJS and 20 times faster than SJCL) > AES-CBC: 81 MiB/s (3 times faster than CryptoJS and 8 times faster than SJCL)

Edit: The Web Cryptography API is now implemented in most browsers and should be used as the primary solution if you care about performance. Be aware that IE11 implemented an earlier draft version of the standard which did not use promises.

Some examples can be found here:

Solution 8 - Javascript

Use CryptoJS

Here's the code: https://github.com/odedhb/AES-encrypt

And here's an online working example: https://odedhb.github.io/AES-encrypt/

Solution 9 - Javascript

Try asmcrypto.js — it's really fast.

PS: I'm an author and I can answer your questions if any. Also I'd be glad to get some feedback :)

Solution 10 - Javascript

If you are trying to use javascript to avoid using SSL, think again. There are many half-way measures, but only SSL provides secure communication. Javascript encryption libraries can help against a certain set of attacks, but not a true man-in-the-middle attack.

The following article explains how to attempt to create secure communication with javascript, and how to get it wrong: https://stackoverflow.com/questions/5145147/use-javascript-encryption-module-instead-of-ssl-https/9930665#9930665

Note: If you are looking for SSL for google app engine on a custom domain, take a look at wwwizer.com.

Solution 11 - Javascript

There is also a Stanford free lib as an alternative to Cryptojs

http://crypto.stanford.edu/sjcl/

Solution 12 - Javascript

Solution 13 - Javascript

Another solution w/AES-256 support: https://github.com/digitalbazaar/forge

Solution 14 - Javascript

Here is the only solution that worked for me:

http://www.hanewin.net/encrypt/aes/aes.htm

It's pretty basic, but simple to use and seems to work well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoderView Question on Stackoverflow
Solution 1 - Javascriptbackslash17View Answer on Stackoverflow
Solution 2 - JavascriptCheesoView Answer on Stackoverflow
Solution 3 - JavascriptDanny CView Answer on Stackoverflow
Solution 4 - JavascriptSamir TalwarView Answer on Stackoverflow
Solution 5 - JavascriptmarczView Answer on Stackoverflow
Solution 6 - JavascriptDavid KieransView Answer on Stackoverflow
Solution 7 - JavascriptJoelView Answer on Stackoverflow
Solution 8 - JavascriptOded BreinerView Answer on Stackoverflow
Solution 9 - JavascriptvibornoffView Answer on Stackoverflow
Solution 10 - JavascriptspeedplaneView Answer on Stackoverflow
Solution 11 - JavascriptJaiView Answer on Stackoverflow
Solution 12 - JavascriptKirtanView Answer on Stackoverflow
Solution 13 - JavascriptdlongleyView Answer on Stackoverflow
Solution 14 - JavascriptCpnCrunchView Answer on Stackoverflow