zoukankan      html  css  js  c++  java
  • nodejs实现端到端加密

    本文引用 https://www.jianshu.com/p/0ade7f83d12e

    端到端加密的实现主要依据两个主要算法: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;
    

    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);
    

      

  • 相关阅读:
    margin折叠及hasLayout && Block Formatting Contexts
    视口(viewport)原理详解之第二部分(移动端浏览器)
    Git & SSH
    .NET Core 3.0 System.Text.Json 和 Newtonsoft.Json 行为不一致问题及解决办法
    阿里云函数计算 .NET Core 初体验
    Oracle PLSQL 存储过程无法进入单步调试
    解释器错误:没有那个文件或目录
    不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。
    Oracle 11G R2 安装图解
    Oracle UTL_HTTP
  • 原文地址:https://www.cnblogs.com/pig1314/p/11620900.html
Copyright © 2011-2022 走看看