aes-256-cbc encrypt/decrypt

node v6.17.1
version: 2.0.0
endpointsharetweet
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))
Loading…

no comments

    sign in to comment