decipher.final()은 해독 객체의 값을 포함하는 버퍼 또는 문자열을 반환하는 데 사용됩니다. 암호화 모듈 내에서 Cipher 클래스가 제공하는 내장 메소드 중 하나입니다. decipher.final 메소드가 호출되면 decipher 메소드를 사용하여 데이터를 복호화할 수 없습니다. cipher.final 메소드를 두 번 이상 호출하면 오류가 발생합니다.
구문
decipher.final([outputEncoding])
매개변수
위의 매개변수는 다음과 같이 설명됩니다. -
-
출력인코딩 – 출력 인코딩을 매개변수로 사용합니다. 이 매개변수의 입력 유형은 문자열입니다. 가능한 입력 값은 16진수, base64 등입니다.
예시
이름이 decipherFinal.js인 파일을 만들고 아래 코드 조각을 복사합니다. 파일을 생성한 후 다음 명령을 사용하여 아래 예와 같이 이 코드를 실행하십시오 -
node decipherFinal.js
decipherFinal.js
// Example to demonstrate the use of cipher.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 cipher object to get cipher
const encrypted1 =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';
let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');
// let decryptedValue1 = decipher.update(encrypted1, 'hex', 'utf8');
decryptedValue1 += decipher.final('utf8');
// Printing the result...
console.log("Decrypted value -- " + decryptedValue1);
// console.log("Base64 String:- " + base64Value) 출력
C:\home\node>> node decipherFinal.js Decrypted value -- Welcome to tutorials point
예시
예를 하나 더 살펴보겠습니다.
// Example to demonstrate the use of cipher.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 cipher object to get cipher
const encrypted =
'a05e87569f3f04234812ae997da5684944c32b8776fae676b4abe9074b31cd2a';
// const encrypted2 = '8d11772fce59f08e7558db5bf17b3112';
var buf = [];
// Updating the decopher data
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
// Pushinf the data into buffer after decryption
buf.push(decrypted);
buf.push(decipher.final('utf8'));
// Printing the result
console.log(buf.join(' ')); 출력
C:\home\node>> node decipherFinal.js Welcome to tutor ials point