類: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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。