程序员的资源宝库

网站首页 > gitee 正文

nodejs实现端到端加密(nodejs 密码加密)

sanyeah 2024-04-01 11:21:07 gitee 3 ℃ 0 评论

端到端加密的实现主要依据两个主要算法:1. diffie-hellman密钥交换算法(上文提到过)2.AES(-CBC)对称加密算法

主要流程如下:

  1. 两台设备各生成一对diffie-hellman公私钥。
  2. 在网络上交换公钥。
  3. 两台设备根据自己的私钥和对方的公钥,生成一个新的、相同的密钥。
  4. 利用这个密钥,两台设备可以加密和解密需要传输的内容。

* 这种方式的关键在于,除两台设备外,其他任何人不能获取AES加密密钥。

 

具体实现:

1>、DiffieHellman.js

const crypto = require(‘crypto‘);
class DiffieHellman extends crypto.DiffieHellman {
  // 为避免两端传递prime,使用确定的prime和generator
  constructor(
    prime = ‘c23b53d262fa2a2cf2f730bd38173ec3‘,
    generator = ‘05‘
  ) {
    console.log(‘---- start ----‘)
    super(prime, ‘hex‘, generator, ‘hex‘);
  }

  // 生成密钥对,返回公钥
  getKey() {
    console.log(‘---- start1111 ----‘)
    return this.generateKeys(‘base64‘);
  }

  // 使用对方公钥生成密钥
  getSecret(otherPubKey) {
    console.log(‘---- start2222 ----‘)
    return this.computeSecret(otherPubKey, ‘base64‘, ‘hex‘);
  }

  static createPrime(encoding=null, primeLength=128, generator=2) {  //primeLength 素数p的长度  generator 素数a
    const dh = new crypto.DiffieHellman(primeLength, generator);
    return dh.getPrime(encoding);
  }
}

module.exports = DiffieHellman;

 

2>、AESCrypter.js

const crypto = require(‘crypto‘);

const algorithm = ‘aes-256-cbc‘;

class AESCrypter {
  constructor() {}

  // AES加密
  static encrypt(key, iv, data) {
    iv = iv || "";
    const clearEncoding = ‘utf8‘;
    const cipherEncoding = ‘base64‘;
    const cipherChunks = [];
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    cipher.setAutoPadding(true);
    cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
    cipherChunks.push(cipher.final(cipherEncoding));
    return cipherChunks.join(‘‘);
  }

  // AES解密
  static decrypt(key, iv, data) {
    if (!data) {
      return "";
    }
    iv = iv || "";
    const clearEncoding = ‘utf8‘;
    const cipherEncoding = ‘base64‘;
    const cipherChunks = [];
    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    decipher.setAutoPadding(true);
    cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));
    cipherChunks.push(decipher.final(clearEncoding));
    return cipherChunks.join(‘‘);
  }
}
module.exports = AESCrypter;

资源搜索网站大全 http://www.szhdn.com

3>、checkAES.js

const AESCrypter = require(‘./AESCrypter‘);
const DiffieHellman = require(‘./DiffieHellman‘);

const dh = new DiffieHellman();
const clientPub = dh.getKey();

const dh2 = new DiffieHellman();
const serverPud = dh2.getKey();


console.log(‘--- 公钥1 ---‘, clientPub);
console.log(‘--- 公钥2 ---‘, serverPud);

console.log(‘两端密钥:‘);
const key1 = dh.getSecret(serverPud);   //依据公钥生成秘钥
const key2 = dh2.getSecret(clientPub);
// key1 === key2
console.log(‘---- 秘钥1 ---‘,key1);
console.log(‘---- 秘钥2 ---‘,key2);


const key = key1;
const iv = ‘2624750004598718‘;

const data = ‘在任何一种计算机语言中,输入/输出都是一个很重要的部分。‘;

const encrypted = AESCrypter.encrypt(key, iv, data);
const decrypted = AESCrypter.decrypt(key, iv, encrypted);

console.log(‘-- 加密结果 --‘, encrypted);
console.log(‘-- 解密结果 --‘, decrypted);

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表