💽

Node.jsで文字列を暗号化する

2022/02/04に公開

今回はタイトルの通りNodeJSで文字列を暗号化する方法です。

超簡単です。

暗号化

const crypto = require('crypto')
const cipher = crypto.createCipher('aes-256-cbc', "ここにパスワード")
const crypted = cipher.update("ここに暗号化する文字列", 'utf-8', 'hex')
const crypted_text = crypted + cipher.final('hex')

console.log(crypted_text)

復号化

const crypto = require('crypto')
const decipher = crypto.createDecipher('aes-256-cbc', "ここにパスワード")
const decrypted = decipher.update("ここに復号化する文字列", 'hex', 'utf-8')
const decrypted_text = decrypted + decipher.final('utf-8')

console.log(decrypted_text)

Discussion