Esempio n. 1
0
        /// <summary>
        /// 获取公钥的内容
        /// </summary>
        /// <returns></returns>
        public async Task <string> GetPublicKeyStringAsync()
        {
            var pubKey = await cacheMemory.GetAsync <string>(RSAConfig.Cache_PublicKey);

            if (string.IsNullOrWhiteSpace(pubKey))
            {
                await CreateRSACacheAsync();

                pubKey = await cacheMemory.GetAsync <string>(RSAConfig.Cache_PublicKey);
            }
            return(RsaKeyConvert.PublicKeyXmlToPem(pubKey));
        }
Esempio n. 2
0
        /// <summary>
        /// 生成秘钥
        /// </summary>
        private static void InitRsa()
        {
            var handle = RSA.Create();

            PublicKey  = handle.ExportParameters(false);
            PrivateKey = handle.ExportParameters(true);

            PublicKeyString = RsaKeyConvert.PublicKeyXmlToPem(
                handle.ToXmlString(false));
            PrivateKeyString = RsaKeyConvert.PrivateKeyXmlToPkcs1(
                handle.ToXmlString(true));
        }
Esempio n. 3
0
 /// <summary>
 /// XML-> Pkcs1
 /// </summary>
 /// <param name="publicKeyXml">公钥</param>
 /// <returns></returns>
 public static string ConvertPublicKeyXmlToPkcs1(string publicKeyXml)
 {
     if (publicKeyXml is null)
     {
         return("null");
     }
     try
     {
         return(RsaKeyConvert.PublicKeyXmlToPem(publicKeyXml));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Esempio n. 4
0
 //封装方法,实现委托
 public static void ConversionRSAKeyFunc(string privateKey, string publicKey, EnumConversionType enumConversionType)
 {
     if (enumConversionType == EnumConversionType.XML转化为Pkcs1)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyXmlToPkcs1(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyXmlToPem(privateKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.XML转化为Pkcs8)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyXmlToPkcs8(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyXmlToPem(privateKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs1转化为XML)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs1ToXml(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyPemToXml(publicKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs1转化为Pkcs8)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("No conversion required");
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs8转化为XML)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs8ToXml(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyPemToXml(publicKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs8ToPkcs1(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("No conversion required");
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
 }
Esempio n. 5
0
 /// <summary>
 /// XML-> Pkcs8
 /// </summary>
 public void XmlToPkcs8()
 {
     PrivateKey = RsaKeyConvert.PrivateKeyXmlToPkcs8(PrivateKey);
     PublicKey  = RsaKeyConvert.PublicKeyXmlToPem(PublicKey);
 }
Esempio n. 6
0
        private int OnExecute(CommandLineApplication app)
        {
            if (app.Options.Count == 1 && app.Options[0].ShortName == "h")
            {
                app.ShowHelp();
                return(0);
            }

            if (KeyType == "pub" && ((SourceFormat == "pkcs1" && TargetFormat == "pkcs8") ||
                                     (SourceFormat == "pkcs8" && TargetFormat == "pkcs1")))
            {
                app.Out.WriteLine("This public key does not need to be converted.");
                return(1);
            }

            if (SourceFormat == TargetFormat)
            {
                app.Out.WriteLine("Target format can not equal Source format.");
                return(1);
            }

            try
            {
                string result     = string.Empty;
                string keyContent = File.ReadAllText(KeyFilePath);

                if (KeyType == "pri")
                {
                    switch ($"{SourceFormat}->{TargetFormat}")
                    {
                    case "xml->pkcs1":
                        result = RsaKeyConvert.PrivateKeyXmlToPkcs1(keyContent);
                        break;

                    case "xml->pkcs8":
                        result = RsaKeyConvert.PrivateKeyXmlToPkcs8(keyContent);
                        break;

                    case "pkcs1->xml":
                        result = RsaKeyConvert.PrivateKeyPkcs1ToXml(keyContent);
                        break;

                    case "pkcs1->pkcs8":
                        result = RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(keyContent);
                        break;

                    case "pkcs8->xml":
                        result = RsaKeyConvert.PrivateKeyPkcs8ToXml(keyContent);
                        break;

                    case "pkcs8->pkcs1":
                        result = RsaKeyConvert.PrivateKeyPkcs8ToPkcs1(keyContent);
                        break;
                    }
                }
                else
                {
                    result = SourceFormat == "xml" ? RsaKeyConvert.PublicKeyXmlToPem(keyContent) : RsaKeyConvert.PublicKeyPemToXml(keyContent);
                }

                if (!Directory.Exists(Output))
                {
                    Directory.CreateDirectory(Output);
                }
                string fileName = $"{new FileInfo(KeyFilePath).Name}.new.key";
                string savePath = Path.Combine(Output, fileName);
                File.WriteAllText(savePath, result);
                app.Out.WriteLine($"Process success.File saved in {savePath}.");
            }
            catch (Exception e)
            {
                app.Out.WriteLine($"Process error.Detail message:{e.Message}");
                return(1);
            }

            return(0);
        }
Esempio n. 7
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();
        }