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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM