Node.js学习(8)- 密钥算法模块crypto

1.MD5算法

MD5是一种摘要算法,用于校验内容是否被修改。

2.SHA

md5是哈希算法的一种,哈希算法还有sha1,sha256,sha512

3.Hmac

hmac也是一种哈希算法,但是它需要密钥

4.AES

aes被称为是下一代对称密钥算法(DES),对称算法就是加解密的秘钥都是一样都,过程可逆。

5.Diffie-Hellman

Diffie-Hellman是一种密钥交换协议,用于在不泄露密钥的情况下协商出一个一致密钥使用。大致算法过程如下:

  1. A先选一个素数和一个底数,例如,素数p=23,底数g=5(底数可以任选),再选择一个秘密整数a=6,计算A=g^a mod p=8,然后大声告诉B:p=23,g=5,A=8

  2. B收到小明发来的pgA后,也选一个秘密整数b=15,然后计算B=g^b mod p=19,并大声告诉A:B=19

  3. A自己计算出s=B^a mod p=2,B也自己计算出s=A^b mod p=2,因此,最终协商的密钥s2

6.RSA

rsa是非对称密钥算法,加解密过程不可逆,公钥用于加密,私钥用于解密。

在使用Node进行RSA加密前,我们先要准备好私钥和公钥。

首先,在命令行执行以下命令以生成一个RSA密钥对:

openssl genrsa -aes256 -out rsa-key.pem 2048

根据提示输入密码,这个密码是用来加密RSA密钥的,加密方式指定为AES256,生成的RSA的密钥长度是2048位。执行成功后,我们获得了加密的rsa-key.pem文件。

第二步,通过上面的rsa-key.pem加密文件,我们可以导出原始的私钥,命令如下:

openssl rsa -in rsa-key.pem -outform PEM -out rsa-prv.pem

输入第一步的密码,我们获得了解密后的私钥。

类似的,我们用下面的命令导出原始的公钥:

openssl rsa -in rsa-key.pem -outform PEM -pubout -out rsa-pub.pem

这样,我们就准备好了原始私钥文件rsa-prv.pem和原始公钥文件rsa-pub.pem,编码格式均为PEM。

 

7.代码实现及执行

"use strict";
//引用crypto模块
const crypto = require("crypto");
//-------------MD5 可以任意多次调用update(),update()默认字符串编码是UTF-8
const hash = crypto.createHash("md5");
hash.update("hello, world!");
console.log(hash.digest("hex"));
//--------------SHA1
const sha1 = crypto.createHash("sha1");
sha1.update("hello,world!");
console.log(sha1.digest("hex"));
//-------------SHA256
const sha256 = crypto.createHash("sha256");
sha256.update("hello,world!");
console.log(sha256.digest("hex"));
//------------SHA512
const sha512 = crypto.createHash("sha512");
sha512.update("hello,world!");
console.log(sha512.digest("hex"));
//------------Hmac
const hmac = crypto.createHash("sha256","secret-key");
hmac.update("hello world!");
console.log(hmac.digest("hex"));
//-----------AES
function aesEncrypt(data, key) {
    const cipher = crypto.createCipher("aes192", key);
    var crypted = cipher.update(data, "utf8", "hex");
    crypted += cipher.final("hex");
    return crypted;
}
function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher("aes192", key);
    var decrypted = decipher.update(encrypted, "hex", "utf8");
    decrypted += decipher.final("utf8");
    return decrypted;
}
var data = "hello this a secret message!";
var key = "password";
var encrypted = aesEncrypt(data,key);
var decrypted = aesDecrypt(encrypted, key);

console.log("plain text:" + data);
console.log("Encrypted text:" +encrypted);
console.log(("Decrypted text:" + decrypted));
//------------------diffie-hellman
var ming = crypto.createDiffieHellman(512);
var ming_keys = ming.generateKeys();
var prime = ming.getPrime();
var generator = ming.getGenerator();
console.log("Prime:" + prime.toString("hex"));
console.log("Generator:" + generator.toString("hex") );

var hong = crypto.createDiffieHellman(prime, generator);
var hong_keys = hong.generateKeys();

var ming_secret = ming.computeSecret(hong_keys);
var hong_secret = hong.computeSecret(ming_keys);

console.log("Secret of xiao ming:" + ming_secret.toString("hex"));
console.log("Secret of xiao hong:" + hong_secret.toString("hex"));
//----------RSA
const fs = require("fs");
//从文件加载key
function loadKey(file) {
    return fs.readFileSync(file,"utf8");
};
let  prvKey = loadKey("./rsa-prv.pem");
let  pubKey = loadKey("./rsa-pub.pem");
let  message = "hello world!";
//使用私钥加密公钥解密
let enc_by_prv = crypto.privateEncrypt(prvKey,Buffer.from(message,"utf8"));
console.log("encrypted by private key:" + enc_by_prv.toString("hex"));
let  dec_by_pub = crypto.publicDecrypt(pubKey, enc_by_prv);
console.log("decrypted by public key:" + dec_by_pub.toString("utf8"));
//使用公钥进行加密私钥解密
let enc_by_pub = crypto.publicEncrypt(pubKey,Buffer.from(message,"utf8"));
console.log("encrypted by public key:" + enc_by_pub.toString("hex"));
let dec_by_prv = crypto.privateDecrypt(prvKey,enc_by_pub);
console.log("decrypted by private key:" + dec_by_prv.toString("utf8"));



 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值