예제 #1
0
파일: Program.cs 프로젝트: xflu/SM2
        public static void TestSm2GetKeyPair()
        {
            SM2Utils   sm2Utils = new SM2Utils();
            ECPoint    pubk;
            BigInteger prik;

            SM2Utils.GenerateKeyPair(out pubk, out prik);
            PubKey = Encoding.ASCII.GetString(Hex.Encode(pubk.GetEncoded())).ToUpper();
            PriKey = Encoding.ASCII.GetString(Hex.Encode(prik.ToByteArray())).ToUpper();
            //System.Console.Out.WriteLine("公钥: " + Encoding.ASCII.GetString(Hex.Encode(publicKey.GetEncoded())).ToUpper());
            //System.Console.Out.WriteLine("私钥: " + Encoding.ASCII.GetString(Hex.Encode(privateKey.ToByteArray())).ToUpper());
        }
예제 #2
0
        private static byte[] SignData(FiscoTransModel trans, ECKeyPair key)
        {
            byte[][] b = new byte[10][];
            b[0] = trans.data.AccountNonce;
            b[1] = LibraryHelper.FromBigInteger(trans.data.Price);
            b[2] = LibraryHelper.FromBigInteger(trans.data.GasLimit);
            b[3] = LibraryHelper.FromBigInteger(trans.data.BlockLimit);
            b[4] = trans.data.Recipient;
            b[5] = trans.data.Amount == null ? null : LibraryHelper.FromBigInteger(trans.data.Amount);
            b[6] = trans.data.Payload;
            b[7] = LibraryHelper.FromBigInteger(trans.data.ChainID);
            b[8] = LibraryHelper.FromBigInteger(trans.data.GroupID);
            b[9] = trans.data.ExtraData;

            var txb = Nethereum.RLP.RLP.EncodeElementsAndList(b);

            if (trans.smcrypto)
            {
                BigInteger r, s;
                SM2Utils.Sign(SM2Utils.SM3Hash(txb), key.prik, out r, out s);
                trans.data.R = LibraryHelper.FromBigInteger(r);
                trans.data.S = LibraryHelper.FromBigInteger(s);

                var    pub1 = key.pubk.Q.XCoord.GetEncoded();
                var    pub2 = key.pubk.Q.YCoord.GetEncoded();
                byte[] v    = new byte[pub1.Length + pub2.Length];
                Array.Copy(pub1, 0, v, 0, pub1.Length);
                Array.Copy(pub2, 0, v, pub1.Length, pub2.Length);
                Console.WriteLine(string.Format("sign.V:{0}", Hex.ToHexString(v)));
                trans.data.V = v;
            }
            else
            {
                var bytes = EthUtils.ConvertSHA256byte(txb);
                var sign  = EthUtils.Sign(bytes, key.prik);
                trans.data.R = sign.Item1;
                trans.data.S = sign.Item2;
                trans.data.V = sign.Item3;
            }
            byte[][] si = new byte[b.Length + 3][];
            Array.Copy(b, 0, si, 0, b.Length);
            si[b.Length]     = trans.data.V;
            si[b.Length + 1] = trans.data.R;
            si[b.Length + 2] = trans.data.S;

            var txs = Nethereum.RLP.RLP.EncodeElementsAndList(si);

            return(txs);
        }
예제 #3
0
        public static void TestSm2Enc()
        {
            string testStr = "hello world";

            Console.WriteLine("原始数据 : " + testStr);
            byte[] sourceData = Encoding.ASCII.GetBytes(testStr);
            byte[] pubKey     = HexStringToByteArray(PubKey);
            string encStr     = SM2Utils.Encrypt(pubKey, sourceData);

            Console.WriteLine("加密后数据 : " + encStr);

            byte[] prik        = HexStringToByteArray(PriKey);
            var    data        = Hex.Decode(Encoding.ASCII.GetBytes(encStr));
            var    decodedData = SM2Utils.Decrypt(prik, data);

            var decodedStr = Encoding.ASCII.GetString(decodedData);

            Console.WriteLine("解密后数据 : " + decodedStr);
        }
예제 #4
0
파일: Program.cs 프로젝트: xflu/SM2
        public static void tesdDecFile()
        {
            string     filePath = @"D:\ProjectDemo\SM2Crypto\tmp\MA05M6KK9201810311620120201.enc";
            FileStream fs       = new FileStream(filePath, FileMode.Open);

            byte[] data = new byte[fs.Length];
            fs.Seek(0, SeekOrigin.Begin);
            fs.Read(data, 0, (int)fs.Length);
            fs.Close();

            byte[] prik = Encoding.ASCII.GetBytes(PRV_KEY);
            //var data = Hex.Decode(Encoding.ASCII.GetBytes(encStr));
            var decodedData = SM2Utils.Decrypt(Hex.Decode(prik), data);

            string     zipFilePath = @"D:\ProjectDemo\SM2Crypto\test\MA05M6KK9201810311620120201.zip";
            FileStream zfs         = new FileStream(zipFilePath, FileMode.Create);

            zfs.Write(decodedData, 0, decodedData.Length);
            zfs.Close();
        }
예제 #5
0
        private static byte[] SignData(CitaTransaction trans, bool sm, ECKeyPair key)
        {
            var tx = Util.MarshalByte(trans);

            if (sm)
            {
                var publicKeyStr = fillStr64(formatHexString(Hex.ToHexString(key.pubk.Q.XCoord.ToBigInteger().ToByteArray())))
                                   + fillStr64(formatHexString(Hex.ToHexString(key.pubk.Q.YCoord.ToBigInteger().ToByteArray())));
                Console.WriteLine(string.Format("publicKeyStr:{0}", publicKeyStr));
                BigInteger r, s;
                SM2Utils.Sign(SM2Utils.SM3Hash(tx), key.prik, out r, out s);
                var signStr        = fillStr64(formatHexString(Hex.ToHexString(r.ToByteArray()))) + fillStr64(formatHexString(Hex.ToHexString(s.ToByteArray())));
                var publicKeyBytes = Hex.Decode(publicKeyStr);
                var signBytes      = Hex.Decode(signStr);
                var byteSource     = new byte[signBytes.Length + publicKeyBytes.Length];
                signBytes.CopyTo(byteSource, 0);
                publicKeyBytes.CopyTo(byteSource, signBytes.Length);
                return(byteSource);
            }
            return(null);
        }
        private void BtnSm2Test(object sender, RoutedEventArgs e)
        {
            //公钥
            string publickey = "";
            //私钥
            string privatekey = "";

            //生成公钥和私钥
            SM2Utils.GenerateKeyPair(out publickey, out privatekey);

            System.Console.Out.WriteLine("加密明文: " + "000000");
            System.Console.Out.WriteLine("publickey:" + publickey);
            //开始加密
            string cipherText = SM2Utils.Encrypt(publickey, "000000");

            System.Console.Out.WriteLine("密文: " + cipherText);
            System.Console.Out.WriteLine("privatekey:" + privatekey);
            //解密
            string plainText = SM2Utils.Decrypt(privatekey, cipherText);

            System.Console.Out.WriteLine("明文: " + plainText);
            Console.ReadLine();
        }