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 };