ECCDiffieHellmanCNG Attempting to test implementation with known key pairs from plaintext

Viewed 26

I am using .NET 6's ECDiffieHellmanCng class to generate NISTP256R1, ephemeral key pairs.

Here's my members and constructor:

public class ECDHKeyExchange
    {
        public byte[] myPublicKey;
        
        public ECDiffieHellmanCng diffieHellman;
        public Aes aes;

        public ECDHKeyExchange()
        {
            this.aes = new AesCryptoServiceProvider();

            this.diffieHellman = new ECDiffieHellmanCng(ECCurve.NamedCurves.nistP256)
            {
                KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash,
                HashAlgorithm = CngAlgorithm.Sha256
            };
            

            myPublicKey = this.diffieHellman.PublicKey.ToByteArray();
        }
}

I later use the public key to generate a shared secret with another instance of this class and then encrypt/decrypt a message.

Here's an example:

            string text = "Encrypt Test";
            using (ECDHKeyExchange bob = new ECDHKeyExchange())
            {
                using (ECDHKeyExchange alice = new ECDHKeyExchange())
                {
                    byte[] secretMessage = bob.Encrypt(alice.myPublicKey, text);
                   

                    string decryptedMessage = alice.Decrypt(bob.myPublicKey, secretMessage, bob.aes.IV);

                    if (text == decryptedMessage)
                    {
                        Console.WriteLine("Success");
                    }
                    else
                    {
                        Console.WriteLine("Failure");
                    }
                }
            }

You can find more of this code here: https://www.codeproject.com/Articles/1219531/Implement-Diffie-Hellman-in-Csharp

My question is whether if there's a test implementation that exists that would allow me to take a known set of keys (public1, private1) and (public2, private2) with a sharedSecret and ensure my implementation of encrypt and decrypt are working correctly.

0 Answers
Related