项目作者: ricmoo

项目描述 :
A pure JavaScript implementation of the AES block cipher and all common modes of operation for node.js or web browsers.
高级语言: JavaScript
项目地址: git://github.com/ricmoo/aes-js.git
创建时间: 2015-03-04T07:00:00Z
项目社区:https://github.com/ricmoo/aes-js

开源协议:MIT License

下载


AES-JS

npm version

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.

  1. // aesjs.util.convertStringToBytes(aString)
  2. // Becomes:
  3. aesjs.utils.utf8.toBytes(aString)
  4. // aesjs.util.convertBytesToString(aString)
  5. // Becomes:
  6. aesjs.utils.utf8.fromBytes(aString)

Bytes and Hex strings

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

  1. // aesjs.util.convertStringToBytes(aString, 'hex')
  2. // Becomes:
  3. aesjs.utils.hex.toBytes(aString)
  4. // aesjs.util.convertBytesToString(aString, 'hex')
  5. // Becomes:
  6. 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:

  1. npm install aes-js

And to access it from within node, simply add:

  1. var aesjs = require('aes-js');

Web Browser

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

  1. <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).

  1. // 128-bit, 192-bit and 256-bit keys
  2. var key_128 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
  3. var key_192 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  4. 16, 17, 18, 19, 20, 21, 22, 23];
  5. var key_256 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  6. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
  7. 29, 30, 31];
  8. // or, you may use Uint8Array:
  9. var key_128_array = new Uint8Array(key_128);
  10. var key_192_array = new Uint8Array(key_192);
  11. var key_256_array = new Uint8Array(key_256);
  12. // or, you may use Buffer in node.js:
  13. var key_128_buffer = Buffer.from(key_128);
  14. var key_192_buffer = Buffer.from(key_192);
  15. var key_256_buffer = Buffer.from(key_256);

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.

  1. // An example 128-bit key (16 bytes * 8 bits/byte = 128 bits)
  2. var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
  3. // Convert text to bytes
  4. var text = 'Text may be any length you wish, no padding is required.';
  5. var textBytes = aesjs.utils.utf8.toBytes(text);
  6. // The counter is optional, and if omitted will begin at 1
  7. var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
  8. var encryptedBytes = aesCtr.encrypt(textBytes);
  9. // To print or store the binary data, you may convert it to hex
  10. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  11. console.log(encryptedHex);
  12. // "a338eda3874ed884b6199150d36f49988c90f5c47fe7792b0cf8c7f77eeffd87
  13. // ea145b73e82aefcf2076f881c88879e4e25b1d7b24ba2788"
  14. // When ready to decrypt the hex string, convert it back to bytes
  15. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  16. // The counter mode of operation maintains internal state, so to
  17. // decrypt a new instance must be instantiated.
  18. var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
  19. var decryptedBytes = aesCtr.decrypt(encryptedBytes);
  20. // Convert our bytes back into text
  21. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  22. console.log(decryptedText);
  23. // "Text may be any length you wish, no padding is required."
  1. // An example 128-bit key
  2. var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
  3. // The initialization vector (must be 16 bytes)
  4. var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
  5. // Convert text to bytes (text must be a multiple of 16 bytes)
  6. var text = 'TextMustBe16Byte';
  7. var textBytes = aesjs.utils.utf8.toBytes(text);
  8. var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
  9. var encryptedBytes = aesCbc.encrypt(textBytes);
  10. // To print or store the binary data, you may convert it to hex
  11. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  12. console.log(encryptedHex);
  13. // "104fb073f9a131f2cab49184bb864ca2"
  14. // When ready to decrypt the hex string, convert it back to bytes
  15. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  16. // The cipher-block chaining mode of operation maintains internal
  17. // state, so to decrypt a new instance must be instantiated.
  18. var aesCbc = new aesjs.ModeOfOperation.cbc(key, iv);
  19. var decryptedBytes = aesCbc.decrypt(encryptedBytes);
  20. // Convert our bytes back into text
  21. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  22. console.log(decryptedText);
  23. // "TextMustBe16Byte"

CFB - Cipher Feedback

  1. // An example 128-bit key
  2. var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
  3. // The initialization vector (must be 16 bytes)
  4. var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
  5. // Convert text to bytes (must be a multiple of the segment size you choose below)
  6. var text = 'TextMustBeAMultipleOfSegmentSize';
  7. var textBytes = aesjs.utils.utf8.toBytes(text);
  8. // The segment size is optional, and defaults to 1
  9. var segmentSize = 8;
  10. var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, segmentSize);
  11. var encryptedBytes = aesCfb.encrypt(textBytes);
  12. // To print or store the binary data, you may convert it to hex
  13. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  14. console.log(encryptedHex);
  15. // "55e3af2638c560b4fdb9d26a630733ea60197ec23deb85b1f60f71f10409ce27"
  16. // When ready to decrypt the hex string, convert it back to bytes
  17. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  18. // The cipher feedback mode of operation maintains internal state,
  19. // so to decrypt a new instance must be instantiated.
  20. var aesCfb = new aesjs.ModeOfOperation.cfb(key, iv, 8);
  21. var decryptedBytes = aesCfb.decrypt(encryptedBytes);
  22. // Convert our bytes back into text
  23. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  24. console.log(decryptedText);
  25. // "TextMustBeAMultipleOfSegmentSize"

OFB - Output Feedback

  1. // An example 128-bit key
  2. var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
  3. // The initialization vector (must be 16 bytes)
  4. var iv = [ 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,35, 36 ];
  5. // Convert text to bytes
  6. var text = 'Text may be any length you wish, no padding is required.';
  7. var textBytes = aesjs.utils.utf8.toBytes(text);
  8. var aesOfb = new aesjs.ModeOfOperation.ofb(key, iv);
  9. var encryptedBytes = aesOfb.encrypt(textBytes);
  10. // To print or store the binary data, you may convert it to hex
  11. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  12. console.log(encryptedHex);
  13. // "55e3af2655dd72b9f32456042f39bae9accff6259159e608be55a1aa313c598d
  14. // b4b18406d89c83841c9d1af13b56de8eda8fcfe9ec8e75e8"
  15. // When ready to decrypt the hex string, convert it back to bytes
  16. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  17. // The output feedback mode of operation maintains internal state,
  18. // so to decrypt a new instance must be instantiated.
  19. var aesOfb = new aesjs.ModeOfOperation.ofb(key, iv);
  20. var decryptedBytes = aesOfb.decrypt(encryptedBytes);
  21. // Convert our bytes back into text
  22. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  23. console.log(decryptedText);
  24. // "Text may be any length you wish, no padding is required."

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.

  1. // An example 128-bit key
  2. var key = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ];
  3. // Convert text to bytes
  4. var text = 'TextMustBe16Byte';
  5. var textBytes = aesjs.utils.utf8.toBytes(text);
  6. var aesEcb = new aesjs.ModeOfOperation.ecb(key);
  7. var encryptedBytes = aesEcb.encrypt(textBytes);
  8. // To print or store the binary data, you may convert it to hex
  9. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  10. console.log(encryptedHex);
  11. // "a7d93b35368519fac347498dec18b458"
  12. // When ready to decrypt the hex string, convert it back to bytes
  13. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  14. // Since electronic codebook does not store state, we can
  15. // reuse the same instance.
  16. //var aesEcb = new aesjs.ModeOfOperation.ecb(key);
  17. var decryptedBytes = aesEcb.decrypt(encryptedBytes);
  18. // Convert our bytes back into text
  19. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  20. console.log(decryptedText);
  21. // "TextMustBe16Byte"

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.

  1. // the AES block cipher algorithm works on 16 byte bloca ks, no more, no less
  2. var text = "ABlockIs16Bytes!";
  3. var textAsBytes = aesjs.utils.utf8.toBytes(text)
  4. console.log(textAsBytes);
  5. // [65, 66, 108, 111, 99, 107, 73, 115, 49, 54, 66, 121, 116, 101, 115, 33]
  6. // create an instance of the block cipher algorithm
  7. var key = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3];
  8. var aes = new aesjs.AES(key);
  9. // encrypt...
  10. var encryptedBytes = aes.encrypt(textAsBytes);
  11. console.log(encryptedBytes);
  12. // [136, 15, 199, 174, 118, 133, 233, 177, 143, 47, 42, 211, 96, 55, 107, 109]
  13. // To print or store the binary data, you may convert it to hex
  14. var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
  15. console.log(encryptedHex);
  16. // "880fc7ae7685e9b18f2f2ad360376b6d"
  17. // When ready to decrypt the hex string, convert it back to bytes
  18. var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
  19. // decrypt...
  20. var decryptedBytes = aes.decrypt(encryptedBytes);
  21. console.log(decryptedBytes);
  22. // [65, 66, 108, 111, 99, 107, 73, 115, 49, 54, 66, 121, 116, 101, 115, 33]
  23. // decode the bytes back into our original text
  24. var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
  25. console.log(decryptedText);
  26. // "ABlockIs16Bytes!"

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:

  1. var pbkdf2 = require('pbkdf2');
  2. var key_128 = pbkdf2.pbkdf2Sync('password', 'salt', 1, 128 / 8, 'sha512');
  3. var key_192 = pbkdf2.pbkdf2Sync('password', 'salt', 1, 192 / 8, 'sha512');
  4. 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:

  1. 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 aes-js@ricmoo.com 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