nodejs aes 加解密實例
更新時間:2018年10月10日 14:53:10 作者:adley_app
今天小編就為大家分享一篇nodejs aes 加解密實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
'use strict'; const crypto = require('crypto'); /** * AES加密的配置 * 1.密鑰 * 2.偏移向量 * 3.算法模式CBC * 4.補全值 */ var AES_conf = { key: getSecretKey(), //密鑰 iv: '1012132405963708', //偏移向量 padding: 'PKCS7Padding' //補全值 } /** * 讀取密鑰key * 更具當前客戶端的版本vid、平臺platform獲取對應的key */ function getSecretKey(){ return "abcdabcdabcdabcd"; } /** * AES_128_CBC 加密 * 128位 * return base64 */ function encryption(data) { let key = AES_conf.key; let iv = AES_conf.iv; // let padding = AES_conf.padding; var cipherChunks = []; var cipher = crypto.createCipheriv('aes-128-cbc', key, iv); cipher.setAutoPadding(true); cipherChunks.push(cipher.update(data, 'utf8', 'base64')); cipherChunks.push(cipher.final('base64')); return cipherChunks.join(''); } /** * 解密 * return utf8 */ function decryption(data){ let key = AES_conf.key; let iv = AES_conf.iv; // let padding = AES_conf.padding; var cipherChunks = []; var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); decipher.setAutoPadding(true); cipherChunks.push(decipher.update(data, 'base64', 'utf8')); cipherChunks.push(decipher.final('utf8')); return cipherChunks.join(''); } console.log(encryption('aaaaa4')); console.log(decryption('VuoXtyUolFyPrK50JnNUdw=='));
以上這篇nodejs aes 加解密實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
在windows上用nodejs搭建靜態(tài)文件服務器的簡單方法
這篇文章主要介紹了在windows上用nodejs搭建靜態(tài)文件服務器的簡單方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08