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
        public static bool Verify(string publicKey_x, string publicKey_y, string m_hash, string rs_r, string rs_s, string curve = "")
        {
            publicKey_x = xTool.ConvertTool.RemoveSpace(publicKey_x);
            publicKey_y = xTool.ConvertTool.RemoveSpace(publicKey_y);
            m_hash      = xTool.ConvertTool.RemoveSpace(m_hash);
            rs_r        = xTool.ConvertTool.RemoveSpace(rs_r);
            rs_s        = xTool.ConvertTool.RemoveSpace(rs_s);

            ECDSABase ecdsa = new ECDSABase(new ECCurve(curve));

            BigInteger[] res    = new BigInteger[2];
            int          Length = ECCurve.BitLength / 4;

            res[0] = BigInteger.Parse("00" + rs_r, System.Globalization.NumberStyles.AllowHexSpecifier);
            res[1] = BigInteger.Parse("00" + rs_s, System.Globalization.NumberStyles.AllowHexSpecifier);

            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    b      = ecdsa.VerifySignature(pubKey,
                                                   BigInteger.Parse("00" + m_hash, System.Globalization.NumberStyles.AllowHexSpecifier),
                                                   res);

            return(b);
        }
Ejemplo n.º 3
0
        public static bool Verify(string publicKey_x, string publicKey_y, string data, string rs_r, string rs_s, string id = "")
        {
            publicKey_x = xTool.ConvertTool.RemoveSpace(publicKey_x);
            publicKey_y = xTool.ConvertTool.RemoveSpace(publicKey_y);
            data        = xTool.ConvertTool.RemoveSpace(data);
            rs_r        = xTool.ConvertTool.RemoveSpace(rs_r);
            rs_s        = xTool.ConvertTool.RemoveSpace(rs_s);
            id          = xTool.ConvertTool.RemoveSpace(id);

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

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

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

            if (id == "" || id == null)
            {
                id = "31323334353637383132333435363738";
            }
            int    idLen        = id.Length * 4;
            string idlen_string = idLen.ToString("X");

            while (idlen_string.Length < 4)
            {
                idlen_string = "0" + idlen_string;
            }
            string tmp = idlen_string + id + Util.ToHexString(ECCurve.a, ECCurve.BitLength) + Util.ToHexString(ECCurve.b, ECCurve.BitLength) +
                         Util.ToHexString(ECCurve.G.X, ECCurve.BitLength) + Util.ToHexString(ECCurve.G.Y, ECCurve.BitLength) + publicKey_x + publicKey_y;

            string Za = ALG.GuoMi.SM3(tmp);
            string e  = ALG.GuoMi.SM3(Za + data);

            BigInteger[] res    = new BigInteger[2];
            int          Length = ECCurve.BitLength / 4;

            res[0] = BigInteger.Parse("00" + rs_r, System.Globalization.NumberStyles.AllowHexSpecifier);
            res[1] = BigInteger.Parse("00" + rs_s, System.Globalization.NumberStyles.AllowHexSpecifier);

            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    b      = ecdsa.VerifySignature_SM2(pubKey,
                                                       BigInteger.Parse("00" + e, System.Globalization.NumberStyles.AllowHexSpecifier),
                                                       res);

            return(b);
        }
Ejemplo n.º 4
0
        public static string[] GenerateKeyPair(string curve = "")
        {
            ECDSABase ecdsa = new ECDSABase(new ECCurve(curve));
            ECKey     key   = ecdsa.GenerateKeyPair();

            string[] keyPair = new string[3];

            keyPair[0] = Util.ToHexString(key.PrivateKey, ECCurve.BitLength);
            keyPair[1] = Util.ToHexString(key.PublicKey.X, ECCurve.BitLength);
            keyPair[2] = Util.ToHexString(key.PublicKey.Y, ECCurve.BitLength);
            return(keyPair);
        }
Ejemplo n.º 5
0
        public static string[] Sign(string privateKey, string m_hash, string curve = "")
        {
            privateKey = xTool.ConvertTool.RemoveSpace(privateKey);
            m_hash     = xTool.ConvertTool.RemoveSpace(m_hash);

            ECDSABase ecdsa = new ECDSABase(new ECCurve(curve));

            BigInteger[] res = ecdsa.SignMessage(BigInteger.Parse("00" + privateKey, System.Globalization.NumberStyles.AllowHexSpecifier),
                                                 BigInteger.Parse("00" + m_hash, System.Globalization.NumberStyles.AllowHexSpecifier));
            string[] tmp = new string[2];
            tmp[0] = Util.ToHexString(res[0], ECCurve.BitLength);
            tmp[1] = Util.ToHexString(res[1], ECCurve.BitLength);

            return(tmp);
        }
Ejemplo n.º 6
0
        public static string[] Sign(string privateKey, string data, string id = "")
        {
            privateKey = xTool.ConvertTool.RemoveSpace(privateKey);
            data       = xTool.ConvertTool.RemoveSpace(data);
            id         = xTool.ConvertTool.RemoveSpace(id);

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

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

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

            if (id == "" || id == null)
            {
                id = "31323334353637383132333435363738";
            }
            int idLen = id.Length * 4;

            string [] pubkey       = Util.GetPublicKey(privateKey, "SM2");
            string    idlen_string = idLen.ToString("X");

            while (idlen_string.Length < 4)
            {
                idlen_string = "0" + idlen_string;
            }
            string tmp = idlen_string + id + Util.ToHexString(ECCurve.a, ECCurve.BitLength) + Util.ToHexString(ECCurve.b, ECCurve.BitLength) +
                         Util.ToHexString(ECCurve.G.X, ECCurve.BitLength) + Util.ToHexString(ECCurve.G.Y, ECCurve.BitLength) + pubkey[0] + pubkey[1];

            string Za = ALG.GuoMi.SM3(tmp);
            string e  = ALG.GuoMi.SM3(Za + data);

            BigInteger[] res = ecdsa.SignMessage_SM2(BigInteger.Parse("00" + privateKey, System.Globalization.NumberStyles.AllowHexSpecifier),
                                                     BigInteger.Parse("00" + e, System.Globalization.NumberStyles.AllowHexSpecifier));
            string[] sign_rs = new string[2];
            sign_rs[0] = Util.ToHexString(res[0], ECCurve.BitLength);
            sign_rs[1] = Util.ToHexString(res[1], ECCurve.BitLength);

            return(sign_rs);
        }
Ejemplo n.º 7
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.º 8
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.º 9
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);
        }