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

PHPz
풀어 주다: 2023-08-25 08:13:16
앞으로
1074명이 탐색했습니다.

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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!