Skip to content

Instantly share code, notes, and snippets.

@sweet-zone
Created September 4, 2017 01:45
Show Gist options
  • Select an option

  • Save sweet-zone/264cc4eccb8b3853683aa20a98c473ef to your computer and use it in GitHub Desktop.

Select an option

Save sweet-zone/264cc4eccb8b3853683aa20a98c473ef to your computer and use it in GitHub Desktop.
/**
* 加密解密
*/
var crypto = require('crypto');
var _p = {};
/**
* 加密
* @method encryption
* @param type 加密方法 des-ecb/aes-128-ecb
* @param data 加密的数据
* @param key 加密的 key
* @return 加密后的数据
*/
_p.encryption = function(type, data, key) {
var iv = '';
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var cipher = crypto.createCipheriv(type, key, iv);
cipher.setAutoPadding(true);
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));
return cipherChunks.join('');
}
_p.decryption = function(type, data, key) {
var iv = '';
var clearEncoding = 'utf8';
var cipherEncoding = 'base64';
var cipherChunks = [];
var decipher = crypto.createDecipheriv(type, key, iv);
decipher.setAutoPadding(true);
cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));
cipherChunks.push(decipher.final(clearEncoding));
return cipherChunks.join('');
}
module.exports = _p;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment