@@ -185,7 +185,18 @@ Example: Using `Cipher` objects as streams:
185
185
186
186
``` js
187
187
const crypto = require (' crypto' );
188
- const cipher = crypto .createCipher (' aes192' , ' a password' );
188
+
189
+ const algorithm = ' aes-192-cbc' ;
190
+ const password = ' Password used to generate key' ;
191
+ // Key length is dependent on the algorithm. In this case for aes192, it is
192
+ // 24 bytes (192 bits).
193
+ // Use async `crypto.scrypt()` instead.
194
+ const key = crypto .scryptSync (password, ' salt' , 24 );
195
+ // Use `crypto.randomBytes()` to generate a random iv instead of the static iv
196
+ // shown here.
197
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
198
+
199
+ const cipher = crypto .createCipheriv (algorithm, key, iv);
189
200
190
201
let encrypted = ' ' ;
191
202
cipher .on (' readable' , () => {
@@ -195,7 +206,7 @@ cipher.on('readable', () => {
195
206
});
196
207
cipher .on (' end' , () => {
197
208
console .log (encrypted);
198
- // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
209
+ // Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
199
210
});
200
211
201
212
cipher .write (' some clear text data' );
@@ -207,7 +218,16 @@ Example: Using `Cipher` and piped streams:
207
218
``` js
208
219
const crypto = require (' crypto' );
209
220
const fs = require (' fs' );
210
- const cipher = crypto .createCipher (' aes192' , ' a password' );
221
+
222
+ const algorithm = ' aes-192-cbc' ;
223
+ const password = ' Password used to generate key' ;
224
+ // Use the async `crypto.scrypt()` instead.
225
+ const key = crypto .scryptSync (password, ' salt' , 24 );
226
+ // Use `crypto.randomBytes()` to generate a random iv instead of the static iv
227
+ // shown here.
228
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
229
+
230
+ const cipher = crypto .createCipheriv (algorithm, key, iv);
211
231
212
232
const input = fs .createReadStream (' test.js' );
213
233
const output = fs .createWriteStream (' test.enc' );
@@ -219,12 +239,21 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
219
239
220
240
``` js
221
241
const crypto = require (' crypto' );
222
- const cipher = crypto .createCipher (' aes192' , ' a password' );
242
+
243
+ const algorithm = ' aes-192-cbc' ;
244
+ const password = ' Password used to generate key' ;
245
+ // Use the async `crypto.scrypt()` instead.
246
+ const key = crypto .scryptSync (password, ' salt' , 24 );
247
+ // Use `crypto.randomBytes` to generate a random iv instead of the static iv
248
+ // shown here.
249
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
250
+
251
+ const cipher = crypto .createCipheriv (algorithm, key, iv);
223
252
224
253
let encrypted = cipher .update (' some clear text data' , ' utf8' , ' hex' );
225
254
encrypted += cipher .final (' hex' );
226
255
console .log (encrypted);
227
- // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
256
+ // Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
228
257
```
229
258
230
259
### cipher.final([ outputEncoding] )
@@ -340,7 +369,17 @@ Example: Using `Decipher` objects as streams:
340
369
341
370
``` js
342
371
const crypto = require (' crypto' );
343
- const decipher = crypto .createDecipher (' aes192' , ' a password' );
372
+
373
+ const algorithm = ' aes-192-cbc' ;
374
+ const password = ' Password used to generate key' ;
375
+ // Key length is dependent on the algorithm. In this case for aes192, it is
376
+ // 24 bytes (192 bits).
377
+ // Use the async `crypto.scrypt()` instead.
378
+ const key = crypto .scryptSync (password, ' salt' , 24 );
379
+ // The IV is usually passed along with the ciphertext.
380
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
381
+
382
+ const decipher = crypto .createDecipheriv (algorithm, key, iv);
344
383
345
384
let decrypted = ' ' ;
346
385
decipher .on (' readable' , () => {
@@ -353,8 +392,9 @@ decipher.on('end', () => {
353
392
// Prints: some clear text data
354
393
});
355
394
395
+ // Encrypted with same algorithm, key and iv.
356
396
const encrypted =
357
- ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504 ' ;
397
+ ' e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa ' ;
358
398
decipher .write (encrypted, ' hex' );
359
399
decipher .end ();
360
400
```
@@ -364,7 +404,15 @@ Example: Using `Decipher` and piped streams:
364
404
``` js
365
405
const crypto = require (' crypto' );
366
406
const fs = require (' fs' );
367
- const decipher = crypto .createDecipher (' aes192' , ' a password' );
407
+
408
+ const algorithm = ' aes-192-cbc' ;
409
+ const password = ' Password used to generate key' ;
410
+ // Use the async `crypto.scrypt()` instead.
411
+ const key = crypto .scryptSync (password, ' salt' , 24 );
412
+ // The IV is usually passed along with the ciphertext.
413
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
414
+
415
+ const decipher = crypto .createDecipheriv (algorithm, key, iv);
368
416
369
417
const input = fs .createReadStream (' test.enc' );
370
418
const output = fs .createWriteStream (' test.js' );
@@ -376,10 +424,19 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
376
424
377
425
``` js
378
426
const crypto = require (' crypto' );
379
- const decipher = crypto .createDecipher (' aes192' , ' a password' );
380
427
428
+ const algorithm = ' aes-192-cbc' ;
429
+ const password = ' Password used to generate key' ;
430
+ // Use the async `crypto.scrypt()` instead.
431
+ const key = crypto .scryptSync (password, ' salt' , 24 );
432
+ // The IV is usually passed along with the ciphertext.
433
+ const iv = Buffer .alloc (16 , 0 ); // Initialization vector.
434
+
435
+ const decipher = crypto .createDecipheriv (algorithm, key, iv);
436
+
437
+ // Encrypted using same algorithm, key and iv.
381
438
const encrypted =
382
- ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504 ' ;
439
+ ' e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa ' ;
383
440
let decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
384
441
decrypted += decipher .final (' utf8' );
385
442
console .log (decrypted);
0 commit comments