ECDiffieHellmanCng从Byte获取ECDiffieHellmanPublicKey []

时间:2014-03-06 22:55:33

标签: c# cryptography

我正在为我的服务器实现DiffieHellman密钥交换,但是在我收到转换为Byte[]的另一方公钥后,我无法将其转换回ECDiffieHellmanPublicKey以在{DeriveKeyMaterial中使用它1}}

这是我的代码:

var socket = state.Socket;
var buffer = state.Buffer;
var alice = new ECDiffieHellmanCng
{
    KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
    HashAlgorithm = CngAlgorithm.Sha256
};
var exchangePacket = new ExchangeMessage(144, (int)PacketType.Exchange)
{
    PublicKey = alice.PublicKey.ToByteArray()
};
var len1 = socket.Send(exchangePacket, 0, exchangePacket.Length, SocketFlags.None);
if (0 >= len1) {
    return false;
}
var len2 = socket.Receive(buffer, buffer.Length, SocketFlags.None);
if (len2 <= 0)
    return false;
Array.Resize(ref buffer, len2);
var bob = new ExchangeMessage(buffer); //HOW TO GET ECDiffieHellmanPublicKey FROM MY BYTE[]
byte[] aliceKey = alice.DeriveKeyMaterial(/*Can't get ECDiffieHellmanPublicKey from byte[]*/);

1 个答案:

答案 0 :(得分:0)

这是我如何使它工作

DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromByteArray(bob.PublicKey, new CngKeyBlobFormat("ECCPUBLICBLOB")));