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