2014-03-06 139 views
0

我實現我的服務器,但之後我收到其轉化爲Byte[]對方的公鑰,我不能將其轉換回ECDiffieHellmanPublicKeyDeriveKeyMaterialECDiffieHellmanCng獲取ECDiffieHellmanPublicKey從字節[]

使用它DiffieHellman密鑰交換

這裏是我的代碼:

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[]*/); 
+0

是'LEN2 == len1'? TCP可以將一個'Send'調用分成多個'Receive'調用,或者將多個'Send'調用統一成一個'Receive'調用。所以你必須發送長度,然後循環讀取,直到獲得足夠的數據。 – Loki

+0

http://msdn.microsoft.com/en-us/library/system.security.cryptography.ecdiffiehellmancngpublickey.frombytearray(v=vs.110).aspx – SLaks

+0

@Loki,即使我有一個65535長度的緩衝區? –

回答

0

請問我這是怎麼得到它的工作

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