【发布时间】:2019-10-10 22:17:51
【问题描述】:
我正在使用 EC 曲线 P-256。我生成一个密钥对。 然后,形成私钥,我计算公钥。
由于某种原因,两个公钥值不对应。
查看包含的代码以及生成密钥对的函数:
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;
}
如果您比较生成的公钥和计算的公钥的 (X,Y) 坐标。你会得到不同的价值。 我期望相同的价值! 怎么了?
【问题讨论】:
标签: c# bouncycastle private-key public-key elliptic-curve