C# - 将公钥blob导入ECDiffieHellmanCng

时间:2018-01-30 12:55:49

标签: c# .net cryptography diffie-hellman public-key-exchange

我无法使用ECDiffieHellmanCng类交换密钥:

第1步 - 创建公钥

titatic.csv

第2步 - 交换并获取私钥

public byte[] CreatePublicKey()
{
    using (ECDiffieHellmanCng cng = new ECDiffieHellmanCng())
    {
        cng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
        cng.HashAlgorithm = CngAlgorithm.Sha512;
        return cng.PublicKey.ToByteArray();
    }
}

示例

public byte[] CreatePrivateKey(byte[] publicKey1, byte[] publicKey2)
{
    using(ECDiffieHellmanCng cng = new ECDiffieHellmanCng(CngKey.Import(publicKey1, CngKeyBlobFormat.EccPublicBlob)))
    {
        cng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
        cng.HashAlgorithm = CngAlgorithm.Sha512;
        return cng.DeriveKeyMaterial(CngKey.Import(publicKey2, CngKeyBlobFormat.EccPublicBlob));
    }
}

具体来说,它在byte[] alicePublicKey = CreatePublicKey(); byte[] bobPublicKey = CreatePublicKey(); // This fails byte[] alicePrivateKey = CreatePrivateKey(alicePublicKey, bobPublicKey); byte[] bobPrivateKey = CreatePrivateKey(bobPublicKey, alicePublicKey); 方法的

}方法中失败了。

CreatePrivateKey(...)

错误

  

System.Security.Cryptography.CryptographicException:'键不存在。'

我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题是您尝试使用两个公钥来派生共享密钥(DeriveKeyMaterial的结果)。这不会起作用,因为您需要一方的私钥和另一方的公钥(不需要第一方的公钥,因为它可以从私钥派生)。这是一个例子(我修复了一些术语,因为现在它们具有误导性 - CreatePrivateKey不会创建私钥)。请注意,您通常不会像这样导出私钥,而是将它们存储在容器中,所以这只是例如:

public static (byte[] publicKey, byte[] privateKey) CreateKeyPair() {
    using (ECDiffieHellmanCng cng = new ECDiffieHellmanCng(
        // need to do this to be able to export private key
        CngKey.Create(
            CngAlgorithm.ECDiffieHellmanP256,
            null,
            new CngKeyCreationParameters
                { ExportPolicy = CngExportPolicies.AllowPlaintextExport }))) {
        cng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
        cng.HashAlgorithm = CngAlgorithm.Sha512;
        // export both private and public keys and return
        var pr = cng.Key.Export(CngKeyBlobFormat.EccPrivateBlob);
        var pub = cng.PublicKey.ToByteArray();
        return (pub, pr);
    }
}

public static byte[] CreateSharedSecret(byte[] privateKey, byte[] publicKey) {
    // this returns shared secret, not private key
    // initialize algorithm with private key of one party
    using (ECDiffieHellmanCng cng = new ECDiffieHellmanCng(CngKey.Import(privateKey, CngKeyBlobFormat.EccPrivateBlob))) {
        cng.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
        cng.HashAlgorithm = CngAlgorithm.Sha512;
        // use public key of another party
        return cng.DeriveKeyMaterial(CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob));
    }
}

现在有两个函数:

var aliceKeyPair = CreateKeyPair();
var bobKeyPair = CreateKeyPair();                                
byte[] bobSharedSecret = CreateSharedSecret(bobKeyPair.privateKey, aliceKeyPair.publicKey);
byte[] aliceSharedSecret = CreateSharedSecret(aliceKeyPair.privateKey, bobKeyPair.publicKey);
// derived shared secrets are the same - the whole point of this algoritm
Debug.Assert(aliceSharedSecret.SequenceEqual(bobSharedSecret));