示例#1
0
        public static BigInteger[] Sm2Sign(byte[] md, AsymmetricCipherKeyPair keypair)
        {
            SM3Digest sm3 = new SM3Digest();

            ECPublicKeyParameters ecpub = (ECPublicKeyParameters)keypair.Public;

            byte[] z = SM2CryptoServiceProvider.Instance.Sm2GetZ(Encoding.Default.GetBytes(SM2CryptoServiceProvider.Instance.userId), ecpub.Q);
            sm3.BlockUpdate(z, 0, z.Length);

            byte[] p = md;
            sm3.BlockUpdate(p, 0, p.Length);

            byte[] hashData = new byte[32];
            sm3.DoFinal(hashData, 0);

            // e
            BigInteger e = new BigInteger(1, hashData);
            // k
            BigInteger k     = null;
            ECPoint    kp    = null;
            BigInteger r     = null;
            BigInteger s     = null;
            BigInteger userD = null;

            do
            {
                do
                {
                    ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)keypair.Private;
                    k  = ecpriv.D;
                    kp = ecpub.Q;

                    userD = ecpriv.D;

                    // r
                    r = e.Add(kp.X.ToBigInteger());
                    r = r.Mod(SM2CryptoServiceProvider.Instance.ecc_n);
                }while (r.Equals(BigInteger.Zero) || r.Add(k).Equals(SM2CryptoServiceProvider.Instance.ecc_n));

                // (1 + dA)~-1
                BigInteger da_1 = userD.Add(BigInteger.One);
                da_1 = da_1.ModInverse(SM2CryptoServiceProvider.Instance.ecc_n);
                // s
                s = r.Multiply(userD);
                s = k.Subtract(s).Mod(SM2CryptoServiceProvider.Instance.ecc_n);
                s = da_1.Multiply(s).Mod(SM2CryptoServiceProvider.Instance.ecc_n);
            }while (s.Equals(BigInteger.Zero));

            byte[] btRS = new byte[64];
            byte[] btR  = r.ToByteArray();
            byte[] btS  = s.ToByteArray();
            Array.Copy(btR, btR.Length - 32, btRS, 0, 32);
            Array.Copy(btS, btS.Length - 32, btRS, 32, 32);

            return(new BigInteger[] { r, s });
        }
示例#2
0
        public virtual void Encrypt(byte[] data)
        {
            sm3c3.BlockUpdate(data, 0, data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                if (keyOff == key.Length)
                {
                    NextKey();
                }


                data[i] ^= key[keyOff++];
            }
        }
示例#3
0
        public static string Hash(string str)
        {
            SM3Digest sm3 = new SM3Digest();

            byte[] md   = new byte[sm3.GetDigestSize()];
            byte[] msg1 = Encoding.UTF8.GetBytes(str);
            sm3.BlockUpdate(msg1, 0, msg1.Length);
            sm3.DoFinal(md, 0);
            string s = Encoding.UTF8.GetString(Hex.Encode(md));

            return(s.ToUpper());
        }
示例#4
0
        private void Reset()
        {
            sm3keybase = new SM3Digest();
            sm3c3      = new SM3Digest();


            byte[] p;


            p = p2.X.ToBigInteger().ToByteArray();
            sm3keybase.BlockUpdate(p, 0, p.Length);
            sm3c3.BlockUpdate(p, 0, p.Length);


            p = p2.Y.ToBigInteger().ToByteArray();
            sm3keybase.BlockUpdate(p, 0, p.Length);


            ct = 1;
            NextKey();
        }
        public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
        {
            SM3Digest sm3 = new SM3Digest();

            byte[] p;
            // userId length
            int len = userId.Length * 8;

            sm3.Update((byte)(len >> 8 & 0x00ff));
            sm3.Update((byte)(len & 0x00ff));

            // userId
            sm3.BlockUpdate(userId, 0, userId.Length);

            // a,b
            p = ecc_a.ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);
            p = ecc_b.ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);
            // gx,gy
            p = ecc_gx.ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);
            p = ecc_gy.ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);

            // x,y
            p = userKey.X.ToBigInteger().ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);
            p = userKey.Y.ToBigInteger().ToByteArray();
            sm3.BlockUpdate(p, 0, p.Length);

            // Z
            byte[] md = new byte[sm3.GetDigestSize()];
            sm3.DoFinal(md, 0);

            return(md);
        }
示例#6
0
        public static bool Verify(byte[] msg, byte[] signData, byte[] certData)
        {
            var x5092 = new System.Security.Cryptography.X509Certificates.X509Certificate2(certData);

            byte[] certPK = x5092.GetPublicKey();

            certPK = ByteUtils.SubBytes(certPK, 1, 64);

            byte[] certPKX = ByteUtils.SubBytes(certPK, certPK.Length - 32 - 32, 32);
            byte[] certPKY = ByteUtils.SubBytes(certPK, certPK.Length - 32, 32);


            System.String strcertPKX = ByteUtils.ByteArrayToHex(certPKX);
            System.String strcertPKY = ByteUtils.ByteArrayToHex(certPKY);
            BigInteger    biX        = new BigInteger(strcertPKX, 16);
            BigInteger    biY        = new BigInteger(strcertPKY, 16);


            ECFieldElement x = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biX);
            ECFieldElement y = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biY);

            ECPoint userKey = new FpPoint(SM2CryptoServiceProvider.Instance.ecc_curve, x, y);


            SM3Digest sm3 = new SM3Digest();

            byte[] z = SM2CryptoServiceProvider.Instance.Sm2GetZ(Encoding.Default.GetBytes(SM2CryptoServiceProvider.Instance.userId), userKey);
            sm3.BlockUpdate(z, 0, z.Length);


            byte[] p = msg;
            sm3.BlockUpdate(p, 0, p.Length);

            byte[] md = new byte[32];
            sm3.DoFinal(md, 0);


            byte[] btR = ByteUtils.SubBytes(signData, 0, 32);
            byte[] btS = ByteUtils.SubBytes(signData, 32, 32);


            System.String strR = ByteUtils.ByteArrayToHex(btR);
            System.String strS = ByteUtils.ByteArrayToHex(btS);
            BigInteger    r    = new BigInteger(strR, 16);
            BigInteger    s    = new BigInteger(strS, 16);

            // e_
            BigInteger e = new BigInteger(1, md);
            // t
            BigInteger t = r.Add(s).Mod(SM2CryptoServiceProvider.Instance.ecc_n);

            if (t.Equals(BigInteger.Zero))
            {
                return(false);
            }

            // x1y1
            ECPoint x1y1 = SM2CryptoServiceProvider.Instance.ecc_point_g.Multiply(s);

            x1y1 = x1y1.Add(userKey.Multiply(t));

            // R
            BigInteger R = e.Add(x1y1.X.ToBigInteger()).Mod(SM2CryptoServiceProvider.Instance.ecc_n);

            return(r.Equals(R));
        }