簡體   English   中英

C# 服務器和 Python 客戶端之間的 Diffie Hellman

[英]Diffie Hellman between C# Server and Python Client

我正在開發一個 C#-Server,它應該與 Python-Client 通信。 我想使用 AES 加密消息,並且我想使用 Diffie Hellmann 進行密鑰交換。

當我嘗試我的服務器加載客戶端發送的公鑰時,我得到一個 CryptographicException 告訴我參數錯誤。

這是我的 C# 代碼:

using System.Security.Cryptography;

//...

ECDiffieHellmanCng dh = new ECDiffieHellmanCng();
dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
dh.HashAlgorithm = CngAlgorithm.Sha256;
BigInteger integer = BigInteger.Parse(key);  // key (string) contains the public key sent by the client
// Here is the error:
CngKey foreignKey = CngKey.Import(integer.ToByteArray(), CngKeyBlobFormat.EccPublicBlob);
byte[] key = dh.DeriveKeyMaterial(foreignKey);

Python代碼:

import pyDH
d1 = pyDH.DiffieHellman()
pubkey = d1.gen_public_key()  // This is the huge prime number which gets sent to the server

您可以在https://pypi.org/project/pyDH/找到 Python 庫

謝謝回答!

我認為 pyDH 使用 (RSA) DH,而 ECDiffieHellmannCng 默認使用橢圓曲線,即 NIST P-521。 嘗試使用類似 tinyec 的 python package,並為客戶端和服務器選擇相同的曲線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM