C#服务器和Python客户端之间的差异

2024-05-19 12:02:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个C#服务器,它将与Python客户机通信。我想用AES加密消息,对于密钥交换,我想用Diffie Hellmann

当我尝试我的服务器加载客户端发送的公钥时,我得到一个加密异常,它告诉我参数是错误的

这是我的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/

谢谢你的回答


Tags: thekey代码服务器isintegerpublicsent

热门问题