Spaces:
Running
Running
File size: 962 Bytes
034dfd3 |
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 |
const crypto = require('crypto')
const 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 {
const [ivBase64, encryptedData] = encrypted.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.");
return null;
}
}
module.exports = { encrypt, decrypt }; |