Created
September 4, 2017 01:45
-
-
Save sweet-zone/264cc4eccb8b3853683aa20a98c473ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 加密解密 | |
| */ | |
| 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