public static RSACryptoServiceProvider ImportPemKey(this RSACryptoServiceProvider provider, string pem) { var parameters = PemKeyUtils.GetParametersFromPemString(pem); if (parameters == null) { throw new FormatException("PEM格式不正确"); } provider.ImportParameters(parameters.Value); return(provider); }
//public static string ToXmlKey(this RSACryptoServiceProvider provider, bool includePrivateParameters) //{ // RSAParameters rsaParams = provider.ExportParameters(includePrivateParameters); // StringBuilder sb = new StringBuilder(); // sb.Append("<RSAKeyValue>"); // sb.Append("<Modulus>" + Convert.ToBase64String(rsaParams.Modulus) + "</Modulus>"); // sb.Append("<Exponent>" + Convert.ToBase64String(rsaParams.Exponent) + "</Exponent>"); // if (includePrivateParameters) // { // sb.Append("<P>" + Convert.ToBase64String(rsaParams.P) + "</P>"); // sb.Append("<Q>" + Convert.ToBase64String(rsaParams.Q) + "</Q>"); // sb.Append("<DP>" + Convert.ToBase64String(rsaParams.DP) + "</DP>"); // sb.Append("<DQ>" + Convert.ToBase64String(rsaParams.DQ) + "</DQ>"); // sb.Append("<InverseQ>" + Convert.ToBase64String(rsaParams.InverseQ) + "</InverseQ>"); // sb.Append("<D>" + Convert.ToBase64String(rsaParams.D) + "</D>"); // } // sb.Append("</RSAKeyValue>"); // return (sb.ToString()); //} public static string ToPemPublicKey(this RSACryptoServiceProvider provider) { return(PemKeyUtils.ExportPublicKey(provider)); }