Ejemplo n.º 1
0
        public static string Encrypt(string publicKey_x, string publicKey_y, string data)
        {
            publicKey_x = xTool.ConvertTool.RemoveSpace(publicKey_x);
            publicKey_y = xTool.ConvertTool.RemoveSpace(publicKey_y);
            data        = xTool.ConvertTool.RemoveSpace(data);

            if (data.Length % 2 != 0)
            {
                throw new Exception("Invalid Data");
            }

            ECDSABase ecdsa = new ECDSABase(new ECCurve("SM2"));

            string c1, c2, c3, t;

            c1 = c2 = c3 = t = "";
            string x2, y2;

            x2 = y2 = "";

            //publicKey_x = "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A";
            //publicKey_y = "75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42";

            BigInteger x          = BigInteger.Parse("00" + publicKey_x, System.Globalization.NumberStyles.AllowHexSpecifier);
            BigInteger y          = BigInteger.Parse("00" + publicKey_y, System.Globalization.NumberStyles.AllowHexSpecifier);
            ECPoint    pubkey     = new ECPoint(x, y);
            bool       isContinue = true;

            while (isContinue)
            {
                //BigInteger k = BigInteger.Parse("004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F", System.Globalization.NumberStyles.AllowHexSpecifier);
                //data = "656E6372797074696F6E207374616E64617264";
                BigInteger k      = ECCMath.RandomIntegerBelow(ECCurve.n);
                ECPoint    point1 = ECCMath.ScalarMult(k, ECCurve.G);
                c1 = Util.ToHexString(point1.X, ECCurve.BitLength) + Util.ToHexString(point1.Y, ECCurve.BitLength);

                ECPoint S = ECCMath.ScalarMult(ECCurve.h, pubkey);
                if (ECCMath.IsInfinityPoint(S))
                {
                    throw new Exception("S is Infinity Point");
                }

                ECPoint point2 = ECCMath.ScalarMult(k, pubkey);
                x2 = Util.ToHexString(point2.X, ECCurve.BitLength);
                y2 = Util.ToHexString(point2.Y, ECCurve.BitLength);

                t = KDF(x2 + y2, data.Length * 4);
                for (int i = 0; i < t.Length; i++)
                {
                    if (t.Substring(i, 1) != "0")
                    {
                        isContinue = false;
                        break;
                    }
                }
            }
            c2 = XOR(data, t);
            c3 = ALG.GuoMi.SM3(x2 + data + y2);
            return(c1 + c3 + c2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates signature.
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public BigInteger[] SignMessage(BigInteger privateKey, BigInteger e)
        {
            BigInteger r = 0;
            BigInteger s = 0;

            while (r == 0 || s == 0)
            {
                BigInteger k     = ECCMath.RandomIntegerBelow(ECCurve.n);
                ECPoint    point = ECCMath.ScalarMult(k, ECCurve.G);

                r = ECCMath.MathMod(point.X, ECCurve.n);
                s = ECCMath.MathMod((e + r * privateKey) * ECCMath.InverseMod(k, ECCurve.n), ECCurve.n);
            }

            BigInteger[] bigInteger = new BigInteger[3];
            bigInteger[0] = r;
            bigInteger[1] = s;
            return(bigInteger);
        }
Ejemplo n.º 3
0
        public ECKey(ECCurve eCCurve = null)
        {
            Curve = eCCurve;

            /*
             * BigInteger P = BigInteger.Parse("00fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger A = BigInteger.Parse("0000", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger B = BigInteger.Parse("0007", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gx = BigInteger.Parse("0079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gy = BigInteger.Parse("00483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger N = BigInteger.Parse("00fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", System.Globalization.NumberStyles.AllowHexSpecifier);
             * var H = 1;
             */

            //SM2

            /*
             * BigInteger P = BigInteger.Parse("00FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger A = BigInteger.Parse("00FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger B = BigInteger.Parse("0028E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gx = BigInteger.Parse("0032C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gy = BigInteger.Parse("00BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger N = BigInteger.Parse("00FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", System.Globalization.NumberStyles.AllowHexSpecifier);
             * var H = 1;
             *
             *
             *
             * //512bit
             * BigInteger P = BigInteger.Parse("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger A = BigInteger.Parse("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger B = BigInteger.Parse("0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gx = BigInteger.Parse("00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger Gy = BigInteger.Parse("011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650", System.Globalization.NumberStyles.AllowHexSpecifier);
             * BigInteger N = BigInteger.Parse("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", System.Globalization.NumberStyles.AllowHexSpecifier);
             * var H = 1;
             *
             *
             * Curve = new ECCurve(P, A, B, new ECPoint(Gx,Gy), N, H);*/

            PrivateKey = ECCMath.RandomIntegerBelow(ECCurve.n - 2);
            PublicKey  = ECCMath.ScalarMult(PrivateKey, ECCurve.G);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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]);
        }
Ejemplo n.º 8
0
        public static string Decrypt(string privateKey, string data)
        {
            privateKey = xTool.ConvertTool.RemoveSpace(privateKey);
            data       = xTool.ConvertTool.RemoveSpace(data);

            if (data.Length % 2 != 0)
            {
                throw new Exception("Invalid Cipher");
            }

            if (data.Length < 96 * 2)
            {
                throw new Exception("Invalid Cipher");
            }

            ECDSABase ecdsa = new ECDSABase(new ECCurve("SM2"));

            string c1, c2, c3, t;

            c1 = data.Substring(0, 128);
            c3 = data.Substring(128, 64);
            c2 = data.Substring(128 + 64);
            t  = "";
            string x2, y2;

            x2 = y2 = "";
            bool isZero = true;

            BigInteger x  = BigInteger.Parse("00" + c1.Substring(0, 64), System.Globalization.NumberStyles.AllowHexSpecifier);
            BigInteger y  = BigInteger.Parse("00" + c1.Substring(64), System.Globalization.NumberStyles.AllowHexSpecifier);
            ECPoint    C1 = new ECPoint(x, y);

            if (!ECCMath.IsOnCurve(C1))
            {
                throw new Exception("SM2 Decrypt Failed, C1 Not On Curve");
            }

            ECPoint S = ECCMath.ScalarMult(ECCurve.h, C1);

            if (ECCMath.IsInfinityPoint(S))
            {
                throw new Exception("S is Infinity Point");
            }

            BigInteger prikey = BigInteger.Parse("00" + privateKey, System.Globalization.NumberStyles.AllowHexSpecifier);

            ECPoint point2 = ECCMath.ScalarMult(prikey, C1);

            x2 = Util.ToHexString(point2.X, ECCurve.BitLength);
            y2 = Util.ToHexString(point2.Y, ECCurve.BitLength);

            t = KDF(x2 + y2, c2.Length * 4);

            for (int i = 0; i < t.Length; i++)
            {
                if (t.Substring(i, 1) == "0")
                {
                    continue;
                }
                else
                {
                    isZero = false;
                    break;
                }
            }

            if (isZero)
            {
                throw new Exception("t = 0");
            }

            string plaindata = XOR(c2, t);

            string u = ALG.GuoMi.SM3(x2 + plaindata + y2);

            if (!u.Equals(c3, StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("SM2 Decrypt Failed, u != C3");
            }

            return(plaindata);
        }