/// <summary> /// 使用现有秘钥覆盖生成秘钥 /// </summary> /// <param name="publicKey">公钥</param> /// <param name="privateKey">私钥</param> public static void InitRsaByKey(string publicKey, string privateKey) { PublicKeyString = string.IsNullOrEmpty(publicKey) ? PublicKeyString : publicKey; PrivateKeyString = string.IsNullOrEmpty(privateKey) ? PrivateKeyString : privateKey; ImportKeyPair(RsaKeyConvert.PublicKeyPemToXml(PublicKeyString), RsaKeyConvert.PrivateKeyPkcs1ToXml(PrivateKeyString)); }
/// <summary> /// Pkcs1-> XML /// </summary> /// <param name="privateKeyXml">私钥</param> /// <returns></returns> public static string ConvertPrivateKeyPkcs1ToXml(string privateKeyXml) { if (privateKeyXml is null) { return("null"); } try { return(RsaKeyConvert.PrivateKeyPkcs1ToXml(privateKeyXml)); } catch (Exception ex) { return(ex.Message); } }
//封装方法,实现委托 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("请输入正确的公私钥"); } } }
/// <summary> /// Pkcs1-> XML /// </summary> public void Pkcs1ToXml() { PrivateKey = RsaKeyConvert.PrivateKeyPkcs1ToXml(PrivateKey); PublicKey = RsaKeyConvert.PublicKeyPemToXml(PublicKey); }
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); }