import crypto from 'crypto'; import dotenv from 'dotenv'; dotenv.config(); // Laden der Umgebungsvariablen const algorithm = 'aes-256-cbc'; const secretKey = process.env.SECRET_KEY; if (!secretKey || secretKey.length !== 32) { throw new Error('SECRET_KEY length must be 32 bytes'); } const encrypt = (text, iv) => { const cipher = crypto.createCipheriv(algorithm, Buffer.from(secretKey, 'utf-8'), iv); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return encrypted.toString('hex'); }; const decrypt = (encryptedText, iv) => { const encryptedBuffer = Buffer.from(encryptedText, 'hex'); const decipher = crypto.createDecipheriv(algorithm, Buffer.from(secretKey, 'utf-8'), iv); let decrypted = decipher.update(encryptedBuffer); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); }; const generateIv = () => { return crypto.randomBytes(16); }; export { encrypt, decrypt, generateIv };