Maison  >  Article  >  interface Web  >  Méthode decipher.update() dans Node.js

Méthode decipher.update() dans Node.js

PHPz
PHPzavant
2023-08-25 08:13:161078parcourir

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

decipher.update() est utilisé pour mettre à jour le décryptage avec les données reçues selon le format d'encodage donné. C'est l'une des méthodes intégrées fournies par la classe Decipher dans le module crypto. Si le codage d'entrée est spécifié, le paramètre data est une chaîne, sinon le paramètre data est un tampon

Syntaxe

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

Paramètres

Les paramètres ci-dessus sont décrits ci-dessous -

  • data – Il nécessite les données telles qu'elles sont transmises pour mettre à jour l'entrée de contenu décrypté.

  • inputEncoding - Il prend le codage d'entrée comme paramètre. Les valeurs d'entrée possibles sont hexadécimales, base64, etc.

  • outputEncoding – Il prend l'encodage de sortie comme paramètre. Le type d'entrée de ce paramètre est une chaîne. Les valeurs d'entrée possibles sont hexadécimales, base64, etc.

Exemple

Créez un fichier appelé decipherUpdate.js et copiez l'extrait de code suivant. Après avoir créé le fichier, exécutez ce code à l'aide de la commande suivante, comme indiqué dans l'exemple ci-dessous -

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

Regardons un autre exemple.

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

sortie

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer