【问题标题】:calculate public key from private key is not correct从私钥计算公钥不正确
【发布时间】: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


    【解决方案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;
        }
    

    【讨论】:

      【解决方案2】:

      您得到了错误的 X 和 Y 坐标。在内部,点存储在另一个表示(X,Y,Z)中,涉及称为投影坐标的东西。你想要仿射坐标。通过属性 AffineXCoordAffineYCoord 检索等效的 (X, Y) 仿射坐标。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 2014-01-24
        • 2022-01-13
        • 2019-10-12
        相关资源
        最近更新 更多