首页 > web前端 > js教程 > 正文

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

PHPz
发布: 2023-08-25 08:13:16
转载
1075人浏览过

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

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

语法

decipher.update(data, [inputEncoding], [outputEncoding])
登录后复制

参数

以上参数描述如下 -

  • data  – 它需要数据作为传递以更新解密内容的输入。

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

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

示例

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

node decipherUpdate.js
登录后复制

decipherUpdate.js

// Example to demonstrate the use of decipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the decipher object
const key = crypto.scryptSync(password, 'old data', 24);

// Initializing the static iv
const iv = Buffer.alloc(16, 0);

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

// Initializing the decipher object to get decipher
const encrypted = '083bfe1b2f91677e5d00add115be2f1b2e362e190406f5c6b60e86969bf03bff';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';

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

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

// Printing the result...
console.log("Decrypted value -- " + decryptedValue);
// console.log("Base64 String:- " + base64Value)
登录后复制

输出

C:\homeode>> node decipherUpdate.js
Decrypted value -- Some new text data
登录后复制

示例

让我们再看一个示例。

// Example to demonstrate the use of decipher.final() method

// Importing the crypto module
const crypto = require('crypto');

// Initialising the AES algorithm
const algorithm = 'aes-192-cbc';
// Initialising the password used for generating key
const password = '12345678123456789';

// Retrieving key for the decipher object
crypto.scrypt(password, 'salt', 24,
   { N: 512 }, (err, key) => {
      if (err) throw err;

   // Initializing the static 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');

   // Printing the result...
   console.log("Decrypted value:- " + decrypted);
});
登录后复制

输出

C:\homeode>> node decipherUpdate.js
Decrypted value:- Some new text data
登录后复制

以上就是Node.js 中的 decipher.update() 方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号