Esempio n. 1
0
 /// <summary>
 /// Pkcs8-> XML
 /// </summary>
 /// <param name="privateKeyXml">私钥</param>
 /// <returns></returns>
 public static string ConvertPrivateKeyPkcs8ToXml(string privateKeyXml)
 {
     if (privateKeyXml is null)
     {
         return("null");
     }
     try
     {
         return(RsaKeyConvert.PrivateKeyPkcs8ToXml(privateKeyXml));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Esempio n. 2
0
        protected static string Sign(string content, string privateKey)
        {
            privateKey = RsaKeyConvert.PrivateKeyPkcs8ToXml(privateKey);
            byte[] bt     = Encoding.GetEncoding("utf-8").GetBytes(content);
            var    sha256 = new SHA256CryptoServiceProvider();

            byte[] rgbHash = sha256.ComputeHash(bt);

            RSACryptoServiceProvider key = new RSACryptoServiceProvider();

            key.FromXmlString(privateKey);
            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);

            formatter.SetHashAlgorithm("SHA256");
            byte[] inArray = formatter.CreateSignature(rgbHash);
            return(Convert.ToBase64String(inArray));
        }
Esempio n. 3
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. 4
0
 /// <summary>
 /// Pkcs8-> XML
 /// </summary>
 public void Pkcs8ToXml()
 {
     PrivateKey = RsaKeyConvert.PrivateKeyPkcs8ToXml(PrivateKey);
     PublicKey  = RsaKeyConvert.PublicKeyPemToXml(PublicKey);
 }
Esempio n. 5
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);
        }