kaedah decipher.update() dalam Node.js

PHPz
Lepaskan: 2023-08-25 08:13:16
ke hadapan
1077 orang telah melayarinya

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

decipher.update() digunakan untuk mengemas kini penyahsulitan dengan data yang diterima mengikut format pengekodan yang diberikan. Ia adalah salah satu kaedah terbina dalam yang disediakan oleh kelas Decipher dalam modul crypto. Jika pengekodan input ditentukan, parameter data ialah rentetan, jika tidak, parameter data ialah penimbal

Sintaks

decipher.update(data, [inputEncoding], [outputEncoding])
Salin selepas log masuk

Parameter

Parameter di atas diterangkan di bawah -

  • data memerlukan data

    untuk mengemas kini input kandungan yang disulitkan.
  • inputEncoding

    - Ia memerlukan pengekodan input sebagai parameter. Nilai input yang mungkin adalah perenambelasan, asas64, dsb.
  • outputEncoding

    – Ia mengambil pengekodan output sebagai parameter. Jenis input parameter ini ialah rentetan. Nilai input yang mungkin adalah perenambelasan, asas64, dsb.

Contoh

Buat fail bernama decipherUpdate.js dan salin coretan kod berikut. Selepas mencipta fail, jalankan kod ini menggunakan arahan berikut seperti yang ditunjukkan dalam contoh di bawah -

node decipherUpdate.js
Salin selepas log masuk
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)
Salin selepas log masuk

Output

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

Contoh

Mari kita lihat satu lagi contoh.

// 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);
});
Salin selepas log masuk

output

C:\homeode>> node decipherUpdate.js
Decrypted value:- Some new text data
Salin selepas log masuk
🎜

Atas ialah kandungan terperinci kaedah decipher.update() dalam Node.js. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:tutorialspoint.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!