gist

2012年2月19日日曜日

Node.jsで暗号化を使うためのメモ crypto

var crypt = require('crypto');

var cipher = crypt.createCipher('aes256', 'Gufdre89uCXJijdi');
var text = 'この文字列を暗号化します';

var crypted = cipher.update(text, 'utf8', 'hex');
crypted += cipher.final('hex');

console.log('暗号化した文字列: ' + crypted);

var decipher = crypt.createDecipher('aes256', 'Gufdre89uCXJijdi');
var dec = decipher.update(crypted, 'hex', 'utf8');
dec += decipher.final('utf8');

console.log('復号化した文字列: ', dec);

利用できる暗号化アルゴリズムは openssl list-message-digest-algorithms コマンドで確認できます。

0 件のコメント: