Node.js 中的 decipher.update() 方法

的,用于根据给定的编码格式与receivd数据来更新解密。它是加密模块中的 Decipher 类提供的内置方法之一。如果指定了输入编码,则数据参数是字符串,否则数据参数是缓冲区decipher.update()

语法

decipher.update(data, [inputEncoding], [outputEncoding])

参数

上述参数描述如下 -

  • 数据 - 它将数据作为输入,传递给更新解密内容。

  • inputEncoding  – 它将输入编码作为参数。可能的输入值为十六进制、base64 等。

  • outputEncoding  – 它将输出编码作为参数。此参数的输入类型是字符串。可能的输入值为十六进制、base64 等。

示例

创建一个具有名称的文件 -decipherUpdate.js并复制以下代码片段。创建文件后,使用以下命令运行此代码,如下例所示 -

node decipherUpdate.js

解密更新.js

// 演示 decipher.final() 方法使用的示例

// 导入加密模块

const crypto = require('crypto');

// 初始化 AES 算法

const algorithm = 'aes-192-cbc';

// 初始化用于生成密钥的密码

const password = '12345678123456789';

// 检索解密对象的密钥

const key = crypto.scryptSync(password, 'old data', 24);

// 初始化静态iv

const iv = Buffer.alloc(16, 0);

const decipher = crypto.createDecipheriv(algorithm, key, iv);

// 初始化解密对象以获取解密

const encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';

// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';

let decryptedValue = decipher.update(encrypted, 'hex', 'utf8');

// 让decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');

decryptedValue += decipher.final('utf8');

// 打印结果...

console.log("Decrypted value -- " + decryptedValue);

// console.log("Base64 String:- " + base64Value)

输出结果
C:\home\node>> node decipherUpdate.js

Decrypted value -- Some new text data

示例

让我们再看一个例子。

// 演示 decipher.final() 方法使用的示例

// 导入加密模块

const crypto = require('crypto');

// 初始化 AES 算法

const algorithm = 'aes-192-cbc';

// 初始化用于生成密钥的密码

const password = '12345678123456789';

// 检索解密对象的密钥

crypto.scrypt(password, 'salt', 24,

   { N: 512 }, (err, key) => {

      if (err) throw err;

   // 初始化静态iv

   const iv = Buffer.alloc(16, 0);

   // Initializing the decipher with algo, key and iv

   const decipher = crypto.createDecipheriv(algorithm, key, iv);

   const encrypted = '91d6d37e70fbae537715f0a921d15152194435b96ce3973d92fbbc4a82071074';

   //Getting the decrypted string value

   const decrypted = decipher.update(encrypted, 'hex', 'utf8');

   // 打印结果...

   console.log("Decrypted value:- " + decrypted);

});

输出结果
C:\home\node>> node decipherUpdate.js

Decrypted value:- Some new text data

以上是 Node.js 中的 decipher.update() 方法 的全部内容, 来源链接: utcz.com/z/327497.html

回到顶部