decipher.update()는 주어진 인코딩 형식에 따라 수신 데이터로 해독기를 업데이트하는 데 사용됩니다. 암호화 모듈 내 Decipher 클래스에서 제공하는 내장 메소드 중 하나입니다. 입력 인코딩이 지정되면 데이터 인수는 문자열이고, 그렇지 않으면 데이터 인수는 버퍼입니다.
구문
decipher.update(data, [inputEncoding], [outputEncoding])
매개변수
위의 매개변수는 다음과 같이 설명됩니다 -
-
데이터 – 해독 내용을 업데이트하기 위해 전달되는 입력으로 데이터를 취합니다.
-
inputEncoding – 입력 인코딩을 매개변수로 사용합니다. 가능한 입력 값은 16진수, base64 등입니다.
-
출력인코딩 – 출력 인코딩을 매개변수로 사용합니다. 이 매개변수의 입력 유형은 문자열입니다. 가능한 입력 값은 16진수, base64 등입니다.
예시
이름이 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) 출력
C:\home\node>> node decipherUpdate.js Decrypted value -- Some new text data
예시
예를 하나 더 살펴보겠습니다.
// 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:\home\node>> node decipherUpdate.js Decrypted value:- Some new text data