aes-256-cbc encrypt/decrypt
const { createCipheriv, createDecipheriv, randomBytes } = require('crypto')
const key = randomBytes(32)
const iv = randomBytes(16)
function encrypt (string) {
const cipher = createCipheriv('aes-256-cbc', key, iv)
return cipher.update(string, 'utf8', 'hex') + cipher.final('hex')
}
function decrypt (string) {
const decipher = createDecipheriv('aes-256-cbc', key, iv)
return decipher.update(string, 'hex', 'utf8') + decipher.final('utf8')
}
const encryptedString = encrypt('this is a secret message!')
console.log(encryptedString)
console.log(decrypt(encryptedString))
no comments