コード例 #1
0
        public static string[] GetPublicKey(string privatekey, string curve = "")
        {
            privatekey = xTool.ConvertTool.RemoveSpace(privatekey);

            ECDSABase  ecdsa      = new ECDSABase(new ECCurve(curve));
            BigInteger PrivateKey = BigInteger.Parse("00" + privatekey, System.Globalization.NumberStyles.AllowHexSpecifier);

            if (!ECCMath.RangeBetween(PrivateKey, 1, ECCurve.n - 1))
            {
                throw new Exception("Invalid Private Key");
            }
            var PublicKey = ECCMath.ScalarMult(PrivateKey, ECCurve.G);

            string[] key = new string[2];

            key[0] = Util.ToHexString(PublicKey.X, ECCurve.BitLength);
            key[1] = Util.ToHexString(PublicKey.Y, ECCurve.BitLength);

            return(key);
        }
コード例 #2
0
        public bool VerifySignature(ECPoint publicKey, BigInteger e, BigInteger[] signature)
        {
            if (!ECCMath.RangeBetween(signature[0], BigInteger.One, ECCurve.n - 1))
            {
                return(false);
            }
            if (!ECCMath.RangeBetween(signature[1], BigInteger.One, ECCurve.n - 1))
            {
                return(false);
            }

            BigInteger w  = ECCMath.InverseMod(signature[1], ECCurve.n);
            BigInteger u1 = ECCMath.MathMod((e * w), ECCurve.n);
            BigInteger u2 = ECCMath.MathMod((signature[0] * w), ECCurve.n);

            ECPoint point = ECCMath.PointAdd(ECCMath.ScalarMult(u1, ECCurve.G), ECCMath.ScalarMult(u2, publicKey));

            //if (ECCMath.IsInfinityPoint(point))
            //    return false;
            return(ECCMath.MathMod(signature[0], ECCurve.n) == ECCMath.MathMod(point.X, ECCurve.n));
        }
コード例 #3
0
        public static bool VerifyPublickey(string pub_x, string pub_y, string curve = "")
        {
            pub_x = xTool.ConvertTool.RemoveSpace(pub_x);
            pub_y = xTool.ConvertTool.RemoveSpace(pub_y);

            ECDSABase  ecdsa = new ECDSABase(new ECCurve(curve));
            BigInteger x     = BigInteger.Parse("00" + pub_x, System.Globalization.NumberStyles.AllowHexSpecifier);
            BigInteger y     = BigInteger.Parse("00" + pub_y, System.Globalization.NumberStyles.AllowHexSpecifier);

            ECPoint p = new ECPoint(x, y);

            if (ECCMath.IsInfinityPoint(p))
            {
                return(false);
            }

            if (!ECCMath.RangeBetween(x, BigInteger.One, ECCurve.p - 1))
            {
                return(false);
            }
            if (!ECCMath.RangeBetween(y, BigInteger.One, ECCurve.p - 1))
            {
                return(false);
            }

            if (!ECCMath.IsOnCurve(p))
            {
                return(false);
            }

            ECPoint O = ECCMath.ScalarMult(ECCurve.n, p);

            if (!ECCMath.IsInfinityPoint(O))
            {
                return(false);
            }
            //throw new Exception("O is NOT Infinity Point");

            return(true);
        }
コード例 #4
0
        public bool VerifySignature_SM2(ECPoint publicKey, BigInteger e, BigInteger[] signature)
        {
            if (!ECCMath.RangeBetween(signature[0], BigInteger.One, ECCurve.n - 1))
            {
                return(false);
            }
            if (!ECCMath.RangeBetween(signature[1], BigInteger.One, ECCurve.n - 1))
            {
                return(false);
            }

            BigInteger t = ECCMath.MathMod((signature[0] + signature[1]), ECCurve.n);

            if (t.IsZero)
            {
                return(false);
            }
            ECPoint point = ECCMath.PointAdd(ECCMath.ScalarMult(signature[1], ECCurve.G),
                                             ECCMath.ScalarMult(t, publicKey));
            BigInteger R = ECCMath.MathMod((e + point.X), ECCurve.n);

            return(R == signature[0]);
        }