Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

aes-js

> DevOps
Open source

A pure JavaScript implementation of the AES block cipher and all common modes of operation for node.js or web browsers.

1.5K stars0 likes1 views
WebsiteGitHub

About

A pure JavaScript implementation of the AES block cipher and all common modes of operation for node.js or web browsers.

AES-JS

A pure JavaScript implementation of the AES block cipher algorithm and all common modes of operation (CBC, CFB, CTR, ECB and OFB).

Features

  • Pure JavaScript (with no dependencies)
  • Supports all key sizes (128-bit, 192-bit and 256-bit)
  • Supports all common modes of operation (CBC, CFB, CTR, ECB and OFB)
  • Works in either node.js or web browsers

Migrating from 2.x to 3.x

The utility functions have been renamed in the 3.x branch, since they were causing a great deal of confusion converting between bytes and string.

The examples have also been updated to encode binary data as printable hex strings.

Strings and Bytes

Strings should NOT be used as keys. UTF-8 allows variable length, multi-byte characters, so a string that is 16 characters long may not be 16 bytes long.

Also, UTF8 should NOT be used to store arbitrary binary data as it is a string encoding format, not a binary encoding format.

javascript
// aesjs.util.convertStringToBytes(aString)
// Becomes:
aesjs.utils.utf8.toBytes(aString)

// aesjs.util.convertBytesToString(aString)
// Becomes:
aesjs.utils.utf8.fromBytes(aString)

Bytes and Hex strings

Binary data, such as encrypted bytes, can safely be stored and printed as hexidecimal strings.

javascript
// aesjs.util.convertStringToBytes(aString, 'hex')
// Becomes:
aesjs.utils.hex.toBytes(aString)

// aesjs.util.convertBytesToString(aString, 'hex')
// Becomes:
aesjs.utils.hex.fromBytes(aString)

Typed Arrays

The 3.x and above versions of aes-js use Uint8Array instead of Array, which reduces code size when used with Browserify (it no longer pulls in Buffer) and is also about twice the speed.

However, if you need to support browsers older than IE 10, you should continue using version 2.x.

API

Node.js

To install aes-js in your node.js project:

bash
npm install aes-js

And to access it from within node, simply add:

javascript
var aesjs = require('aes-js');

Web Browser

To use aes-js in a web page, add the following:

xml
<script type="text/javascript" src="https://cdn.rawgit.com/ricmoo/aes-js/e27b99df/index.js"></script>

Keys

All keys must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) long.

The library work with Array, Uint8Array and Buffer objects as well as any array-like object (i.e. must have a length property, and have a valid byte value for each entry).

…

To generate keys from simple-to-remember passwords, consider using a password-based key-derivation function such as scrypt or bcrypt.

Common Modes of Operation

There are several modes of operations, each with various pros and cons. In general though, the CBC and CTR modes are recommended. The ECB is NOT recommended., and is included primarily for completeness.

CTR - Counter (recommended)

…

CBC - Cipher-Block Chaining (recommended)

…

CFB - Cipher Feedback

…

OFB - Output Feedback

…

ECB - Electronic Codebook (NOT recommended)

This mode is not recommended. Since, for a given key, the same plaintext block in produces the same ciphertext block out, this mode of operation can leak data, such as patterns. For more details and examples, see the Wikipedia article, Electronic Codebook.

…

Block Cipher

You should usually use one of the above common modes of operation. Using the block cipher algorithm directly is also possible using ECB as that mode of operation is merely a thin wrapper.

But this might be useful to experiment with custom modes of operation or play with block cipher algorithms.

…

Notes

What is a Key

This seems to be a point of confusion for many people new to using encryption. You can think of the key as the "password". However, these algorithms require the "password" to be a specific length.

With AES, there are three possible key lengths, 128-bit (16 bytes), 192-bit (24 bytes) or 256-bit (32 bytes). When you create an AES object, the key size is automatically detected, so it is important to pass in a key of the correct length.

Often, you wish to provide a password of arbitrary length, for example, something easy to remember or write down. In these cases, you must come up with a way to transform the password into a key of a specific length. A Password-Based Key Derivation Function (PBKDF) is an algorithm designed for this exact purpose.

Here is an example, using the popular (possibly obsolete?) pbkdf2:

javascript
var pbkdf2 = require('pbkdf2');

var key_128 = pbkdf2.pbkdf2Sync('password', 'salt', 1, 128 / 8, 'sha512');
var key_192 = pbkdf2.pbkdf2Sync('password', 'salt', 1, 192 / 8, 'sha512');
var key_256 = pbkdf2.pbkdf2Sync('password', 'salt', 1, 256 / 8, 'sha512');

Another possibility, is to use a hashing function, such as SHA256 to hash the password, but this method is vulnerable to Rainbow Attacks, unless you use a salt.

Performance

Todo...

Tests

A test suite has been generated (test/test-vectors.json) from a known correct implementation, pycrypto. To generate new test vectors, run python generate-tests.py.

To run the node.js test suite:

bash
npm test

To run the web browser tests, open the test/test.html file in your browser.

FAQ

How do I get a question I have added?

E-mail me at [email protected] with any questions, suggestions, comments, et cetera.

Donations

Obviously, it's all licensed under the MIT license, so use it as you wish; but if you'd like to buy me a coffee, I won't complain. =)

  • Bitcoin - 1K1Ax9t6uJmjE4X5xcoVuyVTsiLrYRqe2P
  • Ethereum - 0x70bDC274028F3f391E398dF8e3977De64FEcBf04

Issues· 33 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptaesblock-cipherscbccfb

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理