【问题标题】:Diffie Hellman between C# Server and Python ClientC# 服务器和 Python 客户端之间的 Diffie Hellman
【发布时间】:2020-11-28 21:06:21
【问题描述】:

我正在开发一个 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

您可以在以下位置找到 Python 库 https://pypi.org/project/pyDH/

感谢您的回答!

【问题讨论】:

    标签: python c# encryption diffie-hellman


    【解决方案1】:

    我认为 pyDH 使用 (RSA) DH,而 ECDiffieHellmannCng 默认使用椭圆曲线,即 NIST P-521。尝试使用 tinyec 之类的 python 包,并为客户端和服务器选择相同的曲线。

    【讨论】:

    • 不是 RSA DH,它不是一个东西,而是 mod p DH。但你是对的,这两个包使用的是完全不兼容的组。
    猜你喜欢
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多