Spaces:
Running
Running
File size: 1,026 Bytes
6bc1af4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
const crypto = require('crypto');
let PASSWORD = process.env.PASSWORD
const KEY = crypto.createHash('sha256').update(PASSWORD).digest();
const IV_LENGTH = 16;
function encrypt(text) {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-cbc', KEY, iv);
let encrypted = cipher.update(text, 'utf8', 'base64');
encrypted += cipher.final('base64');
return iv.toString('base64') + ':' + encrypted;
}
function decrypt(encrypted) {
try {
// console.log(PASSWORD, encrypted)
const [ivBase64, encryptedData] = encrypted.replace(/ /g, '+').split(':');
const iv = Buffer.from(ivBase64, 'base64');
const decipher = crypto.createDecipheriv('aes-256-cbc', KEY, iv);
let decrypted = decipher.update(encryptedData, 'base64', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
} catch (err) {
console.error("❌ Gagal mendekripsi: kemungkinan password salah atau data rusak.", err);
return null;
}
}
module.exports = { encrypt, decrypt }; |