我正在开发一个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/
谢谢你的回答
我认为pyDH使用(RSA)DH,而ECDiffieHellmannCng使用椭圆曲线,即默认情况下的NIST p-521。尝试使用类似tinyec的python包,并为客户端和服务器选择相同的曲线
相关问题 更多 >
编程相关推荐