반응형
// 함수를 이용한 암호화
const crypto = require('crypto');
function ENCRYPT(arg) { //암호화하여 리턴
const cipher = crypto.createCipher('aes-256-cbc', '사용할Key');
let result = cipher.update(arg, 'utf8', 'base64');
result += cipher.final('base64');
return result;
}
function DECRYPT(arg) { //복호화하여 리턴
const decipher = crypto.createDecipher('aes-256-cbc', '사용할Key');
let result = decipher.update(arg, 'base64', 'utf8'); // 암호화할문 (base64, utf8이 위의 cipher과 반대 순서입니다.)
result += decipher.final('utf8'); // 암호화할문장 (여기도 base64대신 utf8)
return result;
}
function isBase64(text) { //암호화된 데이터인지 판단
const regex = /^[A-Za-z0-9+/]+={0,2}$/;
return regex.test(text); //true:암호화데이터 false:일반데이터
}
console.log(isBase64('HbMtmFdroLU0arLpMflQYtt8xEf4lrPn5tX5k+a8Nzw=')); //true
console.log(isBase64('123456-789456')); //false
console.log(ENCRYPT("758252-3458796"));
console.log(DECRYPT(ENCRYPT("758252-3458796")));
'Javascript' 카테고리의 다른 글
node에서 서버 자신의 아이피 알아내기 (0) | 2023.08.24 |
---|---|
cordova 에서 BLE 통신하기 (0) | 2023.06.22 |
javascript 우클릭 방지 (0) | 2023.02.15 |
자바스크립트 마우스 호버시에 텍스트 출력 (0) | 2023.02.15 |
node 서버 exe파일로 배포 (0) | 2022.12.08 |