nodejs實現端到端加密

2022-09-22 04:06:10 字數 2276 閱讀 8297

本文引用 

端到端加密的實現主要依據兩個主要演算法:1. diffie-hellman金鑰交換演算法(上文提到過)2.aes(-cbc)對稱加密演算法

主要流程如下:

兩台裝置各生成一對diffie-hellman公私鑰。

在網路上交換公鑰。

兩台裝置根據自己的私鑰和對方的公鑰,生成乙個新的、相同的金鑰。

利用這個金鑰,兩台裝置可以加密和解密需要傳輸的內容。

* 這種方式的關鍵在於,除兩台裝置外,其他任何人不能獲取aes加密金鑰。

具體實現:

1>、diffiehellman.js

const crypto = require('crypto');

class diffiehellman extends crypto.diffiehellman

// 生成金鑰對,返回公鑰

getkey()

// 使用對方公鑰生成金鑰

getsecret(otherpubkey)

static createprime(encoding=null, primelength=128, generator=2)

}module.exports = diffiehellman;

2>、aescrypter.js

const crypto = require('crypto');

const algorithm = 'aes-256-cbc';

class aescrypter

// aes加密

static encrypt(key, iv, data)

// aes解密

static decrypt(key, iv, data)

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實現端到端加密

端到端加密的實現主要依據兩個主要演算法 1.diffie hellman金鑰交換演算法 上文提到過 2.aes cbc 對稱加密演算法 主要流程如下 兩台裝置各生成一對diffie hellman公私鑰。在網路上交換公鑰。兩台裝置根據自己的私鑰和對方的公鑰,生成乙個新的 相同的金鑰。利用這個金鑰,兩...

IM 端到端加密

資訊保安領域的大多數專家都承認,端到端加密是確保資料交換安全的最可靠方法之一。按照這種方法,在端到端加密應用之間傳送的訊息只能由這些應用的使用者讀取,任何第三方都無法讀取。通過使用唯一金鑰進行資料加密和解密,可以實現此類功能。只有終端使用者可以生成和儲存這些金鑰。端到端加密系統旨在確保,即使不法分子...

端到端學習

傳統的影象識別問題 將過程分解為預處理,特徵提取和選擇,分類器設計等若干步驟。優點 把複雜的問題分解為簡單 可控且清晰的若干小的子問題。缺點 儘管可以在子問題上得到最優解,但子問題上的最優解並不意味著就能得到全域性問題的最後解。深度學習影象識別 提供了一種端到端的學習正規化 整個學習的流程並不進行人...