none
cryptographic exception caught when using CngKey.Import(byteServerPubKey, CngKeyBlobFormat.EccPublicBlob) RRS feed

  • Question

  • i am trying to have client server communication for performing key exchange mechanism using ECDH. in my client program after receiving server public key it has to use its own public as well as server public key to produce a secret key. but when i import server public key to CngKey object an exception is caught with "Parameter is incorrect".

    here's the part of code

    ECDiffieHellmanCng client= new ECDiffieHellmanCng(256);
                            client.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                            client.HashAlgorithm = CngAlgorithm.Sha256;
                            byte[] clientPublicKey = client.PublicKey.ToByteArray();
    
                            byte[] byteServerPubKey = Encoding.Default.GetBytes(serverPubKey);
    
    CngKey serverPublicKey = CngKey.Import(byteServerPubKey, CngKeyBlobFormat.EccPublicBlob);
    
                            byte[] symmetricKey = client.DeriveKeyMaterial(serverPublicKey);

    please help

    Wednesday, May 28, 2014 4:00 PM

Answers

  • Hi SHASIF,

    The error message shows that the byte array byteServerPubKey is incorrect. So you need to check what method is used to export the CNG public key in Server. I assume you are using the following code in Server.

    byte[] publicBlob = rsaCng.Key.Export(CngKeyBlobFormat.GenericPublicBlob);
    
    String publicKeyBase64 = Convert.ToBase64String(publicBlob);
    

    Try the following code to

    // Decrypt the data that the Metro app encrypted with the exported public key
    
    byte[] dataToDecrypt = Convert.FromBase64String(encryptedDataBase64);
    
    byte[] decryptedData = rsaCng.DecryptValue(dataToDecrypt);
    
    String decryptedString = Encoding.UTF8.GetString(decryptedData);
    

    You can read more information about this process from http://social.msdn.microsoft.com/Forums/windowsapps/en-US/4b96bcd5-39bd-4bd8-85d4-bf689e92e46a/sharing-publicprivate-key-between-net-and-metro?forum=winappswithcsharp.

    Hope useful.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 30, 2014 1:58 AM
    Moderator