I am using EC curve P-256. I generate a key pair. Then, form the private key, I calculate the public key.
For some reason, the two public key values do not correspond.
See included code with the function to generate a key pair:
public static AsymmetricCipherKeyPair Generate_EC_P256_Key_Pair(SecureRandom random)
{
// Select the curve P-256 //
string curveName = "P-256";
X9ECParameters ecP = NistNamedCurves.GetByName(curveName);
ECDomainParameters dom_parameters = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N);
// Generate EC Key Pair //
ECKeyPairGenerator pGen = new ECKeyPairGenerator();
ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(dom_parameters, random);
pGen.Init(genParam);
AsymmetricCipherKeyPair keypair = pGen.GenerateKeyPair();
AsymmetricKeyParameter Priv_key = keypair.Private;
AsymmetricKeyParameter Pub_key = keypair.Public;
ECPrivateKeyParameters private_key = (ECPrivateKeyParameters)keypair.Private;
ECPublicKeyParameters public_key = (ECPublicKeyParameters)keypair.Public;
BigInteger priv_key_exp = private_key.D;
BigInteger test2 = public_key.Q.XCoord.ToBigInteger();
BigInteger test3 = public_key.Q.YCoord.ToBigInteger();
ECPoint pub_key_1 = dom_parameters.G.Multiply(priv_key_exp);
BigInteger test4 = pub_key_1.XCoord.ToBigInteger();
BigInteger test5 = pub_key_1.YCoord.ToBigInteger();
Console.WriteLine("Exponent: " + priv_key_exp.ToString(16));
Console.WriteLine("X-Coord: " + test2.ToString(16));
Console.WriteLine("X-Coord: " + test4.ToString(16));
Console.WriteLine("\n");
Console.WriteLine("Y-Coord: " + test3.ToString(16));
Console.WriteLine("Y-Coord: " + test5.ToString(16));
return keypair;
}
If you compare the (X,Y) coordinates of both the generated public key and the calculated pubic key. You will get a different value. I would expect the same value!! What is wrong?