You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Supports all common modes of operation (CBC, CFB, CTR, ECB and OFB)
14
14
- Works in either node.js or web browsers
15
15
16
+
Migrating from 2.x to 3.x
17
+
-------------------------
18
+
19
+
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.
20
+
21
+
The examples have also been updated to encode binary data as printable hex strings.
22
+
23
+
**Strings and Bytes**
24
+
25
+
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.
26
+
27
+
Also, UTF8 should **NOT** be used to store arbitrary binary data as it is a *string* encoding format, not a *binary* encoding format.
28
+
29
+
```javascript
30
+
// aesjs.util.convertStringToBytes(aString)
31
+
// Becomes:
32
+
aesjs.utils.utf8.toBytes(aString)
33
+
34
+
35
+
// aesjs.util.convertBytesToString(aString)
36
+
// Becomes:
37
+
aesjs.utils.utf8.fromBytes(aString)
38
+
```
39
+
40
+
**Bytes and Hex strings**
41
+
42
+
Binary data, such as encrypted bytes, can safely be stored and printed as hexidecimal strings.
The 3.x and above versions of aes-js use [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.
58
+
59
+
However, if you need to support browsers older than IE 10, you should continue using version 2.x.
60
+
16
61
17
62
API
18
63
===
@@ -36,13 +81,15 @@ var aesjs = require('aes-js');
All keys must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) long. The API's work on either arrays or `Buffer` objects.
90
+
All keys must be 128 bits (16 bytes), 192 bits (24 bytes) or 256 bits (32 bytes) long.
91
+
92
+
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](https://www.npmjs.com/search?q=scrypt) or [bcrypt](https://www.npmjs.com/search?q=bcrypt).
115
+
To generate keys from simple-to-remember passwords, consider using a password-based key-derivation function such as [scrypt](https://www.npmjs.com/package/scrypt-js) or [bcrypt](https://www.npmjs.com/search?q=bcrypt).
68
116
69
117
70
118
Common Modes of Operation
@@ -75,23 +123,33 @@ There are several modes of operations, each with various pros and cons. In gener
75
123
### CTR - Counter (recommended)
76
124
77
125
```javascript
78
-
var key =aesjs.util.convertStringToBytes("Example128BitKey");
126
+
// An example 128-bit key (16 bytes * 8 bits/byte = 128 bits)
// When ready to decrypt the hex string, convert it back to bytes
255
+
var encryptedBytes =aesjs.utils.hex.toBytes(encryptedHex);
256
+
170
257
// The output feedback mode of operation maintains internal state,
171
258
// so to decrypt a new instance must be instantiated.
172
259
var aesOfb =newaesjs.ModeOfOperation.ofb(key, iv);
173
260
var decryptedBytes =aesOfb.decrypt(encryptedBytes);
174
261
175
262
// Convert our bytes back into text
176
-
var decryptedText =aesjs.util.convertBytesToString(decryptedBytes);
263
+
var decryptedText =aesjs.utils.utf8.fromBytes(decryptedBytes);
177
264
console.log(decryptedText);
178
265
// "Text may be any length you wish, no padding is required."
179
266
```
@@ -184,22 +271,31 @@ console.log(decryptedText);
184
271
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](http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Electronic_Codebook_.28ECB.29).
185
272
186
273
```javascript
187
-
var key =aesjs.util.convertStringToBytes("Example128BitKey");
0 commit comments