> 작성일 : 2023-04-06
npm install crypto-js
const encKey = 'opendocsdocs20230626abcdefghij!@'; // 32Byte
const encIV = 'opendocsdocs2023'; // 16Byte
const encAES = (str, key, iv) => {
const cipher = Crypto.AES.encrypt(str, Crypto.enc.Utf8.parse(key), {
iv: Crypto.enc.Utf8.parse(iv),
padding: Crypto.pad.Pkcs7,
mode: Crypto.mode.CBC
});
return cipher.toString();
};
const decAES = (str, key, iv) => {
const cipher = Crypto.AES.decrypt(str, Crypto.enc.Utf8.parse(key), {
iv: Crypto.enc.Utf8.parse(iv),
padding: Crypto.pad.Pkcs7,
mode: Crypto.mode.CBC
});
return cipher.toString(Crypto.enc.Utf8);
};
const encBase64 = (str) => {
const wordArray = Crypto.enc.Utf8.parse(str);
return Crypto.enc.Base64.stringify(wordArray);
};
const decBase64 = (str) => {
const parsedWordArray = Crypto.enc.Base64.parse(str);
return parsedWordArray.toString(Crypto.enc.Utf8);
};
// Enc
const encValue = encAES('7a4d1829-0683-465b-8c79-0de27739d887', encKey, encIV);
const encResult = encBase64(encValue);
console.log(`endode str : ${encResult}`);
// Dec
const devValue = decBase64(encResult);
const decResult = decAES(devValue, encKey, encIV);
console.log(`decode str : ${decResult}`);
Tags : AES256, AES256 암호화, AES256 복호화, 암복호화 샘플, js 암호화, javascript 암호화, js 복호화, javascript 복호화