首頁 > web前端 > js教程 > 主體

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

PHPz
發布: 2023-08-25 08:13:16
轉載
1073 人瀏覽過

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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!