예제 #1
0
        /// <summary>
        /// 签名验证算法
        /// 利用签名者的公钥和传过来的r,s来验证签名是否合法
        /// </summary>
        /// <param name="sm2">sm2对象</param>
        /// <param name="ppk">签名者的公钥16进制字符串</param>
        /// <param name="Z">签名算法产生的16进制字符串 Z</param>
        /// <param name="r">签名算法生成的 R</param>
        /// <param name="s">签名算法生成的 S</param>
        /// <returns></returns>
        public bool Signature_Check(SM2 sm2, string ppk, string Z, string r, string s)
        {
            ECPoint test_p = null;

            //test_p = sm2.userKey;
            //MessageBox.Show(ppk);
            byte[] key = strToToHexByte(ppk);

            test_p = sm2.ecc_curve.DecodePoint(key);
            Com.Itrus.Crypto.SM2.SM2Result sm2Ret = new Com.Itrus.Crypto.SM2.SM2Result();//实例化一个SM2Result的对象sm2Ret
            SM3Digest sm3 = new SM3Digest();

            byte[] z = strToToHexByte(Z);
            sm3.BlockUpdate(z, 0, z.Length);
            byte[] md = new byte[32];
            sm3.DoFinal(md, 0);

            sm2Ret.r = new BigInteger(r, 16);
            sm2Ret.s = new BigInteger(s, 16);
            sm2.Sm2Verify(md, test_p, sm2Ret.r, sm2Ret.s, sm2Ret); //调用Sm2Verify方法,得到R

            if (sm2Ret.r.Equals(sm2Ret.R))                         //如果r==R
            {
                return(true);                                      //System.Console.Out.WriteLine("\n签名结果验证通过!r == R\n");
            }
            else//r!=R
            {
                return(false);//System.Console.Out.WriteLine("\n签名结果验证失败!r != R\n");
            }
        }
예제 #2
0
    public static bool VerifySign(string _dataToSign, string _signPack, string _pubKeyX, string _pubKeyY)
    {
        SM2 sm2 = SM2.Instance;

        sm2.cipher_sm = new SM2.Cipher();

        string signPack1 = _signPack.Substring(0, 64);
        string signPack2 = _signPack.Substring(64, 64);

        BigInteger ecc_gx = new BigInteger(_pubKeyX, 16);
        BigInteger ecc_gy = new BigInteger(_pubKeyY, 16);

        ECFieldElement ecc_gx_fieldElement = sm2.ecc_curve.FromBigInteger(ecc_gx);                                 //选定椭圆曲线上基点 G 的 x 坐标
        ECFieldElement ecc_gy_fieldElement = sm2.ecc_curve.FromBigInteger(ecc_gy);                                 //选定椭圆曲线上基点 G 的 y 坐标
        ECPoint        ecc_point_g         = new FpPoint(sm2.ecc_curve, ecc_gx_fieldElement, ecc_gy_fieldElement); //生成基点 G

        SM2.SM2Result result = new SM2.SM2Result();

        BigInteger gp_br = new BigInteger(signPack1, 16);
        BigInteger gp_bs = new BigInteger(signPack2, 16);

        result.r = gp_br;
        result.s = gp_bs;

        sm2.Sm2Verify(HexStringToBytes(_dataToSign), ecc_point_g, gp_br, gp_bs, result);

        return(result.R.Equals(result.r));
    }