Heim  >  Artikel  >  Web-Frontend  >  decipher.update()-Methode in Node.js

decipher.update()-Methode in Node.js

PHPz
PHPznach vorne
2023-08-25 08:13:161078Durchsuche

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

decipher.update() wird verwendet, um die Entschlüsselung mit den empfangenen Daten gemäß dem angegebenen Kodierungsformat zu aktualisieren. Es handelt sich um eine der integrierten Methoden, die von der Decipher-Klasse im Kryptomodul bereitgestellt werden. Wenn die Eingabekodierung angegeben ist, ist der Datenparameter eine Zeichenfolge, andernfalls ist der Datenparameter ein Puffer

Syntax

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

Parameter

Die oben genannten Parameter werden unten beschrieben -

  • Daten – Es sind die übergebenen Daten erforderlich um die entschlüsselte Inhaltseingabe zu aktualisieren.

  • inputEncoding – Es verwendet die Eingabekodierung als Parameter. Mögliche Eingabewerte sind Hexadezimal, Base64 usw.

  • outputEncoding – Es verwendet die Ausgabekodierung als Parameter. Der Eingabetyp dieses Parameters ist String. Mögliche Eingabewerte sind Hexadezimal, Base64 usw.

Beispiel

Erstellen Sie eine Datei mit dem Namen decipherUpdate.js und kopieren Sie den folgenden Codeausschnitt. Nachdem Sie die Datei erstellt haben, führen Sie diesen Code mit dem folgenden Befehl aus, wie im folgenden Beispiel gezeigt:

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)

Output

C:\homeode>> node decipherUpdate.js
Decrypted value -- Some new text data

Example

Schauen wir uns noch ein Beispiel an.

// 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);
});

Ausgabe

C:\homeode>> node decipherUpdate.js
Decrypted value:- Some new text data

Das obige ist der detaillierte Inhalt vondecipher.update()-Methode in Node.js. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:tutorialspoint.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen