类:Decipher
添加于:v0.1.94
Decipher
类的实例用于解密数据。可以通过以下两种方式之一使用该类:
- 作为可读可写的stream,其中写入明文加密数据以在可读端生成未加密数据,或
- 使用
decipher.update()
decipher.final()
或 crypto.createDecipher()
方法用于创建 crypto.createDecipheriv()
Decipher
实例。 Decipher
对象不能直接使用new
关键字创建。
示例:使用 Decipher
对象作为流:
import { Buffer } from 'node:buffer'; const { scryptSync, createDecipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Key length is dependent on the algorithm. In this case for aes192, it is // 24 bytes (192 bits). // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); let decrypted = ''; decipher.on('readable', () => { while (null !== (chunk = decipher.read())) { decrypted += chunk.toString('utf8'); } }); decipher.on('end', () => { console.log(decrypted); // Prints: some clear text data }); // Encrypted with same algorithm, key and iv. const encrypted = 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; decipher.write(encrypted, 'hex'); decipher.end();
const { scryptSync, createDecipheriv, } = require('node:crypto'); const { Buffer } = require('node:buffer'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Key length is dependent on the algorithm. In this case for aes192, it is // 24 bytes (192 bits). // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); let decrypted = ''; decipher.on('readable', () => { while (null !== (chunk = decipher.read())) { decrypted += chunk.toString('utf8'); } }); decipher.on('end', () => { console.log(decrypted); // Prints: some clear text data }); // Encrypted with same algorithm, key and iv. const encrypted = 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; decipher.write(encrypted, 'hex'); decipher.end();
示例:使用 Decipher
和管道流:
import { createReadStream, createWriteStream, } from 'fs'; import { Buffer } from 'node:buffer'; const { scryptSync, createDecipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); const input = createReadStream('test.enc'); const output = createWriteStream('test.js'); input.pipe(decipher).pipe(output);
const { createReadStream, createWriteStream, } = require('node:fs'); const { scryptSync, createDecipheriv, } = require('node:crypto'); const { Buffer } = require('node:buffer'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); const input = createReadStream('test.enc'); const output = createWriteStream('test.js'); input.pipe(decipher).pipe(output);
示例:使用
和 decipher.update()
方法:decipher.final()
import { Buffer } from 'node:buffer'; const { scryptSync, createDecipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); // Encrypted using same algorithm, key and iv. const encrypted = 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted); // Prints: some clear text data
const { scryptSync, createDecipheriv, } = require('node:crypto'); const { Buffer } = require('node:buffer'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // Use the async `crypto.scrypt()` instead. const key = scryptSync(password, 'salt', 24); // The IV is usually passed along with the ciphertext. const iv = Buffer.alloc(16, 0); // Initialization vector. const decipher = createDecipheriv(algorithm, key, iv); // Encrypted using same algorithm, key and iv. const encrypted = 'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa'; let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); console.log(decrypted); // Prints: some clear text data
相关用法
- Node.js Decipher.final()用法及代码示例
- Node.js Date.isSameDay()用法及代码示例
- Node.js Date.addMinutes()用法及代码示例
- Node.js Domain.run(fn[, ...args])用法及代码示例
- Node.js Date.locale()用法及代码示例
- Node.js Date.format()用法及代码示例
- Node.js Date.addSeconds()用法及代码示例
- Node.js Date.parse()用法及代码示例
- Node.js Domain.bind(callback)用法及代码示例
- Node.js Domain.intercept(callback)用法及代码示例
- Node.js Date.isLeapYeart()用法及代码示例
- Node.js Date.addYears()用法及代码示例
- Node.js Date.preparse()用法及代码示例
- Node.js Date.compile()用法及代码示例
- Node.js Date.transform()用法及代码示例
- Node.js Date.isValid()用法及代码示例
- Node.js Date.addMilliseconds()用法及代码示例
- Node.js DiffieHellman用法及代码示例
- Node.js Date.addDays()用法及代码示例
- Node.js DiffieHellmanGroup用法及代码示例
- Node.js Date.addMonths()用法及代码示例
- Node.js Date.addHours()用法及代码示例
- Node.js Date.subtract()用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
- Node.js http2.Http2ServerRequest request.url用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Decipher。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。