120

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

4

14 Answers 14

71

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

12
  • 38
    JSAES is licensed under GNU GPL - therefore it is not usable for some projects.
    – Robert
    Apr 13, 2011 at 7:57
  • 12
    This is now quite useful and feature rich code.google.com/p/crypto-js Apr 25, 2012 at 4:21
  • 2
    @HappyDeveloper Well, no. Don't blame the license for this. The author has chosen the wrong license, as GPL is not suitable for libraries.
    – inta
    Jun 25, 2013 at 9:13
  • 4
    It's "free of cost to use", but you can't use it if you want to release your resulting product under something other than the GPL.
    – Curtis
    Mar 7, 2014 at 20:31
  • 5
    JSAES is a good starting point but it can be used only to encrypt 16 bytes of data. If you want to encrypt bigger block of data you have to extend it yourself to implement intialization vector, encrypt mode (CBC or other...), padding.
    – Paolo
    Dec 2, 2015 at 16:39
38

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.

11
  • 8
  • 1
    I see one valid use-case - HTML 5 app in which all files are stored localy. If local files can be hijacked then your doomed in any case ;-).
    – Nux
    Jan 9, 2012 at 23:18
  • 8
    about edit3 link, it is a piece of crap of an article... half of its statements are completely false!
    – mjs
    Jan 10, 2012 at 9:07
  • 2
    The random key problem can be addressed by having the user move the mouse and enter keys as a true random generator.
    – mjs
    Jan 10, 2012 at 9:39
  • 3
    The link to the demonstration page appears to be broken.
    – Sean
    Aug 31, 2013 at 9:40
31

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/

3
  • thats what I am talkin about!
    – mjs
    Jan 10, 2012 at 9:07
  • 5
    Documentation is lacking, and it's hard to use. How do you change key length? I hunted round the docs and couldn't figure it out in a reasonable time. Also when you encrypt something you get an array of key-value pairs returned, but the docs don't seem to explain these. I ended up using the movable type library.
    – CpnCrunch
    Oct 24, 2013 at 17:14
  • And this is not async so if you are encrypting or decrypting some longer string for example with AES-CBC, then it blocks the UI
    – rsz
    Sep 20, 2016 at 20:15
13

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

5
  • 1
    I couldn't figure out how to set the IV in that library. Also it's not very OO.
    – Cheeso
    May 13, 2009 at 13:25
  • The equivalent of the IV in counter mode is the nonce. This implementation has been reformulated to be more OO. It does only include counter (CTR) mode of operation.
    – ChrisV
    Oct 1, 2010 at 8:46
  • This works pretty well, except it doesn't have any ECB mode.
    – CpnCrunch
    Oct 24, 2013 at 21:10
  • Googling it also shows up this question as the second result.
    – ublec
    May 4, 2021 at 1:27
  • 1
    You'll have to forgive me; this answer is 12 years old, and Stack Overflow didn't show up on Google very often when I wrote it. :-) May 4, 2021 at 15:45
12

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

4
  • 1
    Unfortunately the documentation is lacking. It only seems to have a 'quick start' guide. Where is the full documentation? It says it supports multiple key lengths, but no documentation on how to do that.
    – CpnCrunch
    Oct 24, 2013 at 17:10
  • @CpnCrunch : the full API doc is not online, but the code as full javadoc api comments. And you can generate it. Read the comments on cipher-core.js source youl find cipher's key size and cipher's IV size.
    – marcz
    Mar 12, 2014 at 11:00
  • weird, when i encrypt and decrypt back, the texts are different
    – OMGPOP
    Mar 19, 2014 at 12:08
  • There are some conflicts because of use openssl
    – Vlad
    Jan 17, 2020 at 11:50
9

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

4
  • 1
    Thank you SO much for this! I couldn't for the life of me get my javascript aes to talk to my python aes.
    – Spike
    Jan 13, 2011 at 19:13
  • 1
    I've been trying all night (with pycrypto and others) to do what your code helped me accomplish in 10 minutes. Thank you SO much! Feb 22, 2011 at 7:04
  • 1
    For whatever reason I got RSA working easily but AES is a royal pain. Thank you for this!!!
    – speedplane
    Mar 22, 2012 at 6:30
  • 2
    the link ... is not found! Jul 8, 2016 at 12:49
8

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:

1
  • I'm getting about 8 MiB/s with CryptoJS. Wonder how this would perform.
    – Lodewijk
    Sep 26, 2014 at 2:33
7

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/

2
  • 1
    the best answer Feb 16, 2018 at 18:50
  • I'm not sure if understood, but your "the code" is different from "online working example" (and "the code" seemed not to work at least in part). Anyway, helped me here. Thanks. May 7, 2022 at 13:50
4

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 :)

3
  • asmcrypto.js is nice, but on IE10 its not works well. it hangs browser for more than 1 min sometimes, or at least 45 sec. Plus, I didn't understand, why you need to replace global Math function? there are lot of libraries which use this one.
    – decho
    Sep 26, 2014 at 11:20
  • 2
    Could you comment on how you're sure it's working properly? IOW: How do you know you implemented AES properly?
    – Lodewijk
    Sep 26, 2014 at 17:37
  • IE10 performance is poor due to it doesn't optimize asm.js at all. Also it has a bit different JIT patterns. Making the code work well in IE also makes it suck in Chrome and FF. Suppose I had right choice. Regarding to Math.random there was a long discussion. Shortly speaking this need for prevention of raw Math.random output leakage (wich in theory may degrade PRNG security).
    – vibornoff
    Sep 29, 2014 at 22:15
3

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

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

3

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: Use JavaScript encryption module instead of SSL/HTTPS

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

2
  • 2
    Not sure why this was downvoted to oblivion, as it is a very good point that anyone thinking of implementing javascript encryption needs to at the very least consider. Upvoted.
    – Jules
    Feb 28, 2014 at 18:41
  • Using client side crypto to avoid SSL is an old point which some people say to go against it: in fact it can add security to HTTPS, avoiding passive attacks, or be used in downloaded applications and browser extensions. I just saw once it being used wrong (my college, but they already fixed it) and many times used right (cryptocat, as a example). Aug 15, 2015 at 20:15
2

http://www.movable-type.co.uk/scripts/aes.html library may be of some help.

2

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

2

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.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.