当前位置:首页 » 密码管理 » 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;
}
};

热点内容
方舟编译器可以用于p20吗 发布:2025-07-04 17:00:17 浏览:783
短片解压 发布:2025-07-04 16:50:08 浏览:736
全工作服务器如何省电 发布:2025-07-04 16:46:00 浏览:993
redis数据库设计 发布:2025-07-04 16:39:23 浏览:334
建设银行账号和密码是什么意思 发布:2025-07-04 16:35:37 浏览:145
feret人脸数据库 发布:2025-07-04 16:35:33 浏览:69
什么游戏要钱不要密码 发布:2025-07-04 16:30:33 浏览:477
安卓雕刻软件叫什么 发布:2025-07-04 16:30:22 浏览:819
mc服务器搭建简单吗 发布:2025-07-04 16:29:23 浏览:142
android动画曲线 发布:2025-07-04 16:16:57 浏览:513