我正在使用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)坐标。您将获得不同的价值。 我期望相同的价值! 怎么了?
答案 0 :(得分:1)
James K Polk向我指出了正确的方向。 在获取坐标之前,我必须先对点进行归一化(“ pub_key_1 = pub_key_1.Normalize()”)。 我相应地更改了代码,现在它给了我正确的结果。
谢谢!
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();
ECPrivateKeyParameters private_key = (ECPrivateKeyParameters)keypair.Private;
ECPublicKeyParameters public_key = (ECPublicKeyParameters)keypair.Public;
BigInteger priv_key_exp = private_key.D;
BigInteger genx = public_key.Q.XCoord.ToBigInteger();
BigInteger geny = public_key.Q.YCoord.ToBigInteger();
BigInteger genx_aff = public_key.Q.AffineXCoord.ToBigInteger();
BigInteger geny_aff = public_key.Q.AffineYCoord.ToBigInteger();
ECPoint pub_key_1 = dom_parameters.G.Multiply(priv_key_exp);
pub_key_1 =pub_key_1.Normalize();
BigInteger calcx = pub_key_1.XCoord.ToBigInteger();
BigInteger calcy = pub_key_1.YCoord.ToBigInteger();
Console.WriteLine("Exponent: " + priv_key_exp.ToString(16));
Console.WriteLine("Generated X-Coord : " + genx.ToString(16));
Console.WriteLine("Generated X-Coord Affine : " + genx_aff.ToString(16));
Console.WriteLine("Calculated X-Coord Affine: " + calcx.ToString(16));
Console.WriteLine("\n");
Console.WriteLine("Generated Y-Coord : " + geny.ToString(16));
Console.WriteLine("Generated Y-Coord Affine : " + geny_aff.ToString(16));
Console.WriteLine("Calculated Y-Coord Affine: " + calcy.ToString(16));
return keypair;
}
答案 1 :(得分:0)
您得到的X和Y坐标错误。在内部,点被存储在涉及投影坐标的替代表示形式(X,Y,Z)中。您需要仿射坐标。通过属性AffineXCoord
和AffineYCoord
检索等效的(X,Y)仿射坐标。