码迷,mamicode.com
首页 > Web开发 > 详细

nodejs实现端到端加密

时间:2019-10-03 23:50:17      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:log   加密   ejs   new   data   pre   方式   两台   push   

本文引用 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);

  

nodejs实现端到端加密

标签:log   加密   ejs   new   data   pre   方式   两台   push   

原文地址:https://www.cnblogs.com/pig1314/p/11620900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!