Exemple #1
0
        /// <summary>
        /// 公钥加密算法,利用下一个用户的公钥进行加密
        /// </summary>
        /// <param name="sm2">sm2对象</param>
        /// <param name="msg">要加密的消息</param>
        /// <param name="ppk">目的签名者的公钥文件夹路径</param>
        /// <param name="id">加密后消息存入的文件夹(最好不存起来)</param>
        public string[] Test_sm2_cipher(SM2 sm2, string msg, string ppk)
        {
            byte[] data = Encoding.Default.GetBytes(msg);//将信息转化为比特

            // 加密过程
            ECPoint userKey = null;

            byte[] key = null;

            ReadpublicKey(out key, ppk);              //读取解密者的公钥
            userKey = sm2.ecc_curve.DecodePoint(key); //把字节形的转化为Ecpoint
            System.String sdata = new UTF8Encoding().GetString(Hex.Encode(data));

            SM2.Cipher cipher = new SM2.Cipher();
            ECPoint    c1     = cipher.Init_enc(sm2, userKey); //调用Init_enc方法

            byte[]        bc1  = c1.GetEncoded();              //将c1的数据类型转换成比特串
            System.String sbc1 = new UTF8Encoding().GetString(Hex.Encode(bc1));

            cipher.Encrypt(data);

            System.String sc2 = new UTF8Encoding().GetString(Hex.Encode(data));
            byte[]        c3  = new byte[32];
            cipher.Dofinal(c3);
            System.String sc3 = new UTF8Encoding().GetString(Hex.Encode(c3));

            string[] cc = { sbc1, sc2, sc3 };
            return(cc);
        }
Exemple #2
0
        /// <summary>
        /// 私钥解密算法
        /// </summary>
        /// <param name="sm2">sm2对象</param>
        /// <param name="pripk">自己的私钥</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        public string deciphering(SM2 sm2, string pripk, string mc1, string sc2, string mc3)
        {
            byte[]     bc1   = new byte[32];
            BigInteger userD = null;

            // String sc2 = null;
            byte[] c3 = new byte[32];
            byte[] data;
            bc1 = strToToHexByte(mc1);
            c3  = strToToHexByte(mc3);
            Readprikey(out userD, pripk);
            // 解密过程
            SM2.Cipher cipher = new SM2.Cipher();
            cipher = new SM2.Cipher();

            ECPoint c1 = sm2.ecc_curve.DecodePoint(bc1);

            data = strToToHexByte(sc2);

            cipher.Init_dec(userD, c1); //调用Init_dec,从c中取出比特串c1,将c1的数据类型转化为椭圆曲线上的点,如果不满足椭圆曲线上的点则报错。
            cipher.Decrypt(data);       //调用Decrypt方法
            //System.String sdata1 = new UTF8Encoding().GetString(Hex.Encode(data));

            string sdata = System.Text.Encoding.Default.GetString(data);

            System.String sc3 = new UTF8Encoding().GetString(Hex.Encode(c3));

            byte[] c3_ = new byte[32];
            cipher.Dofinal(c3_);
            System.String sc3_ = new UTF8Encoding().GetString(Hex.Encode(c3_));
            //数据校验检测数据是否被篡改或丢失
            if (sc3_.ToUpper().Equals(sc3.ToUpper()))//sc3_==sc3
            {
                return(sdata);
            }
            else
            {
                // System.Console.Out.WriteLine("数据校验失败!\n");//sc3_!=sc3
                return("0");
            }
        }