@@ -185,7 +185,18 @@ Example: Using `Cipher` objects as streams:
185185
186186``` js
187187const 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);
189200
190201let encrypted = ' ' ;
191202cipher .on (' readable' , () => {
@@ -195,7 +206,7 @@ cipher.on('readable', () => {
195206});
196207cipher .on (' end' , () => {
197208 console .log (encrypted);
198- // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
209+ // Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
199210});
200211
201212cipher .write (' some clear text data' );
@@ -207,7 +218,16 @@ Example: Using `Cipher` and piped streams:
207218``` js
208219const crypto = require (' crypto' );
209220const 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);
211231
212232const input = fs .createReadStream (' test.js' );
213233const output = fs .createWriteStream (' test.enc' );
@@ -219,12 +239,21 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
219239
220240``` js
221241const 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);
223252
224253let encrypted = cipher .update (' some clear text data' , ' utf8' , ' hex' );
225254encrypted += cipher .final (' hex' );
226255console .log (encrypted);
227- // Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
256+ // Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
228257```
229258
230259### cipher.final([ outputEncoding] )
@@ -340,7 +369,17 @@ Example: Using `Decipher` objects as streams:
340369
341370``` js
342371const 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);
344383
345384let decrypted = ' ' ;
346385decipher .on (' readable' , () => {
@@ -353,8 +392,9 @@ decipher.on('end', () => {
353392 // Prints: some clear text data
354393});
355394
395+ // Encrypted with same algorithm, key and iv.
356396const encrypted =
357- ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504 ' ;
397+ ' e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa ' ;
358398decipher .write (encrypted, ' hex' );
359399decipher .end ();
360400```
@@ -364,7 +404,15 @@ Example: Using `Decipher` and piped streams:
364404``` js
365405const crypto = require (' crypto' );
366406const 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);
368416
369417const input = fs .createReadStream (' test.enc' );
370418const output = fs .createWriteStream (' test.js' );
@@ -376,10 +424,19 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
376424
377425``` js
378426const crypto = require (' crypto' );
379- const decipher = crypto .createDecipher (' aes192' , ' a password' );
380427
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.
381438const encrypted =
382- ' ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504 ' ;
439+ ' e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa ' ;
383440let decrypted = decipher .update (encrypted, ' hex' , ' utf8' );
384441decrypted += decipher .final (' utf8' );
385442console .log (decrypted);
0 commit comments