57
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node.js で AES-256-CBC を使用した暗号化・復号

Last updated at Posted at 2019-03-18

Node.js で AES-256-CBC を使用した暗号化・復号のサンプルです。

#暗号化

function encrypt(algorithm, password, salt, data) {
	
	// 鍵を生成
	const key = crypto.scryptSync(password, salt, 32)
	
	// IV を生成
	const iv = crypto.randomBytes(16)

	// 暗号器を生成
	const cipher = crypto.createCipheriv(algorithm, key, iv)
	
	// data を暗号化
	let encryptedData = cipher.update(data)
	encryptedData = Buffer.concat([encryptedData, cipher.final()])

	return {iv, encryptedData}
}

#復号

function decrypt(algorithm, password, salt, iv, encryptedData) {

	// 鍵を生成
	const key = crypto.scryptSync(password, salt, 32)

	// 復号器を生成
	const decipher = crypto.createDecipheriv(algorithm, key, iv)

	// encryptedData を復号
	let decryptedData = decipher.update(encryptedData)
	decryptedData =  Buffer.concat([decryptedData, decipher.final()])

	return decryptedData
}

#サンプルコード

const crypto = require('crypto')

// AES アルゴリズム		
const ALGO = 'aes-256-cbc'

// 事前に共有すべきパスワード
// console.log(crypto.randomBytes(32).toString('base64'))
const PASSWORD = 'l+/MraaOI1yT3F1l15fJMcEKGiG3iWn7nOTmUS4fWk0='

// 事前に共有すべき SALT
// console.log(crypto.randomBytes(16).toString('base64'))
const SALT = 'kr3dJJ1mPcIKisMOR4RO6w=='

// 暗号化したいメッセージ
const MESSAGE = 'piyopiyo'


// 暗号化メソッド
function encrypt(algorithm, password, salt, data) {
	
	// 鍵を生成
	const key = crypto.scryptSync(password, salt, 32)
	
	// IV を生成
	const iv = crypto.randomBytes(16)

	// 暗号器を生成
	const cipher = crypto.createCipheriv(algorithm, key, iv)
	
	// data を暗号化
	let encryptedData = cipher.update(data)
	encryptedData = Buffer.concat([encryptedData, cipher.final()])

	return {iv, encryptedData}
}


// 復号メソッド
function decrypt(algorithm, password, salt, iv, encryptedData) {

	// 鍵を生成
	const key = crypto.scryptSync(password, salt, 32)

	// 復号器を生成
	const decipher = crypto.createDecipheriv(algorithm, key, iv)

	// encryptedData を復号
	let decryptedData = decipher.update(encryptedData)
	decryptedData =  Buffer.concat([decryptedData, decipher.final()])

	return decryptedData
}


console.log('MESSAGE:', MESSAGE)

// 暗号化したいメッセージ文字列を Buffer に変換
const data = Buffer.from(MESSAGE)
console.log('data:', data)

// 暗号化
let {iv, encryptedData} = encrypt(ALGO, PASSWORD, SALT, data)
console.log('iv:', iv)
console.log('encryptedData:', encryptedData)

// 復号
let decryptedData = decrypt(ALGO, PASSWORD, SALT, iv, encryptedData)
console.log('decryptedData:', decryptedData)
console.log('decryptedData(utf8):', decryptedData.toString('utf-8'))

#メモ

  • 暗号化を行う側と復号を行う側で、事前に安全な経路で PASSWORD と SALT を共有します。
  • iv (initialization vector) は必ず毎回変わるようにします。
  • 暗号化を行う側から、復号を行う側に渡すのは、暗号化されたデータと iv です。
  • 暗号化されたデータと iv は安全でない経路で共有することが可能です。

#Ref.

57
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
57
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?