Node.js の decopher.update() メソッド

PHPz
リリース: 2023-08-25 08:13:16
転載
1074 人が閲覧しました

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

cipher.update() は、指定されたエンコード形式に従って、受信したデータで復号化を更新するために使用されます。これは、crypto モジュールの Decipher クラスによって提供される組み込みメソッドの 1 つです。入力エンコーディングが指定されている場合、データ パラメーターは文字列です。それ以外の場合、データ パラメーターはバッファーです。

Syntax

decipher.update(data, [inputEncoding], [outputEncoding])
ログイン後にコピー

Parameters

上記のパラメーターは次のように説明されます。

  • data – 復号化されたコンテンツを更新するには、入力としてデータが渡される必要があります。

  • inputEncoding - 入力エンコーディングをパラメーターとして受け取ります。可能な入力値は 16 進数、base64 などです。

  • outputEncoding – 出力エンコーディングをパラメータとして受け取ります。このパラメータの入力タイプは文字列です。可能な入力値は 16 進数、base64 などです。

decopherUpdate.js というファイルを作成し、次のコード スニペットをコピーします。ファイルを作成した後、次の例に示すように、次のコマンドを使用してこのコードを実行します -

node decipherUpdate.js
ログイン後にコピー

decpherUpdate.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)
ログイン後にコピー

Output

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 の decopher.update() メソッドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!