Untitled
unknown
plain_text
a year ago
1.1 kB
10
Indexable
const crypto = require('crypto');
// Encryption function
function encrypt(text, key, iv) {
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return encrypted.toString('hex');
}
// Decryption function
function decrypt(text, key, iv) {
let encryptedText = Buffer.from(text, 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
// Example usage
const key = crypto.randomBytes(32); // Key should be 32 bytes for aes-256-cbc
const iv = crypto.randomBytes(16); // Initialization vector should be 16 bytes
const longString = "Your long string here...";
const encryptedString = encrypt(longString, key, iv);
console.log('Encrypted:', encryptedString);
const decryptedString = decrypt(encryptedString, key, iv);
console.log('Decrypted:', decryptedString);Editor is loading...
Leave a Comment