在NodeJS中使用AES等方式进行加密的方法备忘录

执行这样的程序,

crypto = require("crypto");

plainText = 'abcdefghijklmnopqrstuvwxyz';
passowrd = 'WIdlbs9pDHoLnLo4xEnVKc1DKA0XUFS0';
alg = 'aes256'
encoding = 'base64'  // 'binary' or 'hex'

cipher = crypto.createCipher(alg, passowrd);
cipheredText = cipher.update(plainText, 'utf8', encoding);
cipheredText += cipher.final(encoding);

decipher = crypto.createDecipher(alg, passowrd);
dec = decipher.update(cipheredText, encoding, 'utf8');
dec += decipher.final('utf8');

console.log('crypted: '+ cipheredText);
console.log('decrypted: ' + dec);

会变成这样的感觉。

% node memo.js
crypted: 6RM52ZdyXDsi+PtGBTB58L5SlgythPJfVlld2z65YPo=
decrypted: abcdefghijklmnopqrstuvwxyz
广告
将在 10 秒后关闭
bannerAds