>  기사  >  웹 프론트엔드  >  Node.js의 decipher.update() 메서드

Node.js의 decipher.update() 메서드

PHPz
PHPz앞으로
2023-08-25 08:13:161082검색

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

decipher.update()는 주어진 인코딩 형식에 따라 수신된 데이터로 암호 해독을 업데이트하는 데 사용됩니다. crypto 모듈의 Decipher 클래스에서 제공하는 내장 메소드 중 하나입니다. 입력 인코딩이 지정되면 데이터 매개변수는 문자열이고, 그렇지 않으면 데이터 매개변수는 버퍼입니다.

구문

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

매개변수

위 매개변수는 아래에 설명되어 있습니다. -

  • data - 전달된 데이터가 필요합니다. 해독된 콘텐츠 입력을 업데이트합니다.

  • inputEncoding - 입력 인코딩을 매개변수로 사용합니다. 가능한 입력값은 16진수, base64 등입니다.

  • outputEncoding – 출력 인코딩을 매개변수로 사용합니다. 이 매개변수의 입력 유형은 문자열입니다. 가능한 입력값은 16진수, base64 등입니다.

Example

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)

Output

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

Example

예제를 하나 더 살펴보겠습니다.

// 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제