本文介绍了ECDiffieHellmanCng从Byte []获取ECDiffieHellmanPublicKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为我的服务器实现DiffieHellman密钥交换,但是在我收到转换为 Byte []
的另一方公钥后,无法将其转换回 ECDiffieHellmanPublicKey
在 DeriveKeyMaterial中使用它
代码:
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); //如何获取ECDiffieHellmanPublicKey FROM MY BYTE []
byte [] aliceKey = alice.DeriveKeyMaterial(/ *无法从byte [] * /)获取ECDiffieHellmanPublicKey;
这是我如何工作 p>
DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromByteArray(bob.PublicKey,new CngKeyBlobFormat(ECCPUBLICBLOB)));
I am implementing DiffieHellman key exchange for my server but after i receive the other party public key which is converted to Byte[]
i can't convert it back to ECDiffieHellmanPublicKey
to use it in the DeriveKeyMaterial
Here is my code:
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[]*/);
解决方案
Will this is how i got it to work
DeriveKeyMaterial(ECDiffieHellmanCngPublicKey.FromByteArray(bob.PublicKey, new CngKeyBlobFormat("ECCPUBLICBLOB")));
这篇关于ECDiffieHellmanCng从Byte []获取ECDiffieHellmanPublicKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!