當前位置:首頁 » 密碼管理 » node加密

node加密

發布時間: 2024-11-18 21:56:13

① 關於nodejs 怎麼實現 crypto des加密

就是加密和解密使用同一個密鑰,通常稱之為「Session Key 」這種加密技術在當今被廣泛採用,如美國政府所採用的DES加密標准就是一種典型的「對稱式」加密法,它的Session Key長度為56bits。
非對稱式加密:
就是加密和解密所使用的不是同一個密鑰,通常有兩個密鑰,稱為「公鑰」和「私鑰」,它們兩個必需配對使用,否則不能打開加密文件。
加密為系統中經常使用的功能,node自帶強大的加密功能Crypto,下面通過簡單的例子進行練習。
1、加密模塊的引用:
var crypto=require('crypto');
var $=require('underscore');var DEFAULTS = {
encoding: {
input: 'utf8',
output: 'hex'
},
algorithms: ['bf', 'blowfish', 'aes-128-cbc']
};

默認加密演算法配置項:
輸入數據格式為utf8,輸出格式為hex,
演算法使用bf,blowfish,aes-128-abc三種加密演算法;
2、配置項初始化:
function MixCrypto(options) {
if (typeof options == 'string')
options = { key: options };

options = $.extend({}, DEFAULTS, options);
this.key = options.key;
this.inputEncoding = options.encoding.input;
this.outputEncoding = options.encoding.output;
this.algorithms = options.algorithms;
}

加密演算法可以進行配置,通過配置option進行不同加密演算法及編碼的使用。
3、加密方法代碼如下:
MixCrypto.prototype.encrypt = function (plaintext) {
return $.rece(this.algorithms, function (memo, a) {
var cipher = crypto.createCipher(a, this.key);
return cipher.update(memo, this.inputEncoding, this.outputEncoding)
+ cipher.final(this.outputEncoding)
}, plaintext, this);
};

使用crypto進行數據的加密處理。
4、解密方法代碼如下:
MixCrypto.prototype.decrypt = function (crypted) {
try {
return $.receRight(this.algorithms, function (memo, a) {
var decipher = crypto.createDecipher(a, this.key);
return decipher.update(memo, this.outputEncoding, this.inputEncoding)
+ decipher.final(this.inputEncoding);
}, crypted, this);
} catch (e) {
return;
}
};

熱點內容
javawebeclipse編譯 發布:2025-05-14 11:35:24 瀏覽:935
可編程式控制制器試題 發布:2025-05-14 11:25:32 瀏覽:119
dsp混合編程 發布:2025-05-14 11:23:10 瀏覽:248
mysql添加存儲過程 發布:2025-05-14 11:23:01 瀏覽:879
房車旅遊自媒體有腳本嗎 發布:2025-05-14 11:18:18 瀏覽:125
android輸入法鍵盤 發布:2025-05-14 11:15:48 瀏覽:658
谷歌商店安卓手機在哪裡 發布:2025-05-14 11:13:46 瀏覽:535
編程貓銷售女 發布:2025-05-14 11:13:36 瀏覽:335
安卓卡無翼怎麼出小黑屋 發布:2025-05-14 11:13:00 瀏覽:581
買商用筆記本電腦主要看哪些配置 發布:2025-05-14 11:12:15 瀏覽:950