Esempio n. 1
0
        /// <summary>
        /// RSA 加密,解密,签名和验签
        /// </summary>
        public static void RSASignFunc(string privateKey, string publicKey, EnumUserType enumUserType)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            switch (enumUserType)
            {
            case EnumUserType.xml:
                RsaXmlUtil rsaXmlUtil   = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey);
                var        encrypt      = rsaXmlUtil.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
                var        encryptInput = rsaXmlUtil.Decrypt(encrypt, RSAEncryptionPadding.Pkcs1);
                Console.Write("XML加解密是否成功:");
                Console.WriteLine(encryptInput is "123456789");
                var sign       = rsaXmlUtil.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                var verifyData = rsaXmlUtil.VerifyData("987654321", sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                Console.Write("XML签名是否成功");
                Console.WriteLine(verifyData);
                break;

            case EnumUserType.pkcs1:
                RsaPkcs1Util rsaPkcs1Util  = new RsaPkcs1Util(Encoding.UTF8, publicKey, privateKey);
                var          encrypt2      = rsaPkcs1Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
                var          encryptInput2 = rsaPkcs1Util.Decrypt(encrypt2, RSAEncryptionPadding.Pkcs1);
                Console.Write("PKCS1加解密是否成功:");
                Console.WriteLine(encryptInput2 is "123456789");
                var sign2       = rsaPkcs1Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                var verifyData2 = rsaPkcs1Util.VerifyData("987654321", sign2, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                Console.Write("PKCS1签名是否成功");
                Console.WriteLine(verifyData2);
                break;

            default:
                RsaPkcs8Util rsaPkcs8Util  = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey);
                var          encrypt3      = rsaPkcs8Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1);
                var          encryptInput3 = rsaPkcs8Util.Decrypt(encrypt3, RSAEncryptionPadding.Pkcs1);
                Console.Write("PKCS8加解密是否成功:");
                Console.WriteLine(encryptInput3 is "123456789");
                var sign3       = rsaPkcs8Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                var verifyData3 = rsaPkcs8Util.VerifyData("987654321", sign3, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                Console.Write("PKCS8签名是否成功");
                Console.WriteLine(verifyData3);
                break;
            }
        }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            var input = ",130680807779,";
            // 取匹配
            var matches = Regex.Matches(input, @"(1)\d{10}")
                          .Select(m => m.Value)
                          .ToList();

            matches = Regex.Matches(",1306808077701234,", @"(1)\d{10,20}")
                      .Select(m => m.Value)
                      .ToList();

            matches = Regex.Matches("17703430350,,", @"(1)\d{10,20}")
                      .Select(m => m.Value)
                      .ToList();


            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            var str      = "筷开动1234Abc";
            var gbk      = Encoding.GetEncoding("GBK").GetBytes(str);
            var gbkToStr = Encoding.GetEncoding("GBK").GetString(gbk);

            Console.WriteLine($"GBK是否相同:{str == gbkToStr}\n");

            Console.WriteLine("获取Ticks测试------start-------");
            for (var i = 0; i < 10; i++)
            {
                var ticks = DateTime.Now.Ticks.ToString();
                Console.WriteLine($"    {i} ticks:{ticks} len:{ticks.Length}");
            }

            Console.WriteLine("获取Ticks测试------end-------");

            var keyList    = RsaKeyGenerator.XmlKey(2048);
            var privateKey = keyList[0];
            var publicKey  = keyList[1];

            var source      = "1234再看看看!@#¥#!#";
            var rsaXmlUtil  = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey);
            var encryptData = rsaXmlUtil.Encrypt(source, RSAEncryptionPadding.OaepSHA256);

            Console.WriteLine("加密测试:");
            Console.WriteLine("   加密后的数据:{0}", encryptData);
            var decryptData = rsaXmlUtil.Decrypt(encryptData, RSAEncryptionPadding.OaepSHA256);

            Console.WriteLine("\n   解密后的数据:{0}", decryptData);

            Console.WriteLine("   解密后的数据与源数据相同:{0}", source == decryptData);
            Console.WriteLine("\n签名测试:");
            var signData = rsaXmlUtil.SignData(source, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

            Console.WriteLine("   签名值:{0}", signData);
            var verifyRet =
                rsaXmlUtil.VerifyData(source, signData, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

            Console.WriteLine("   验证签名:{0}", verifyRet);

            Console.WriteLine("\n签名转换:");
            var privatePkcs1 = RsaKeyConvert.PrivateKeyXmlToPkcs1(privateKey);
            var publicPkcs1  = RsaKeyConvert.PublicKeyXmlToPem(publicKey);

            Console.WriteLine(" private key of Pkcs1:{0}", privatePkcs1);
            Console.WriteLine(" public key of Pkcs1:{0}", publicPkcs1);

            Console.WriteLine("Guid转换String测试.---开始---");
            var id = Guid.NewGuid();

            var idString = id.ToString("N");

            Console.WriteLine("Guid.ToString(N)={0},原guid:{1}", idString, id);
            var idFromString = Guid.ParseExact(idString, "N");

            Console.WriteLine("Guid.ParseExact(idString,N):{0} ,是否相等:{1}", idFromString, idFromString == id);
            Console.WriteLine("Guid转换String测试.---结束。");
            Console.ReadLine();
        }