netcore2.x下 RSA加解密错误:Operation is not supported on this platform.
代码如下:经过测试,在netcore3.x正常,在netframework下也正常,就是netcore2.x报错。
////// RSA加密 /// /// /// /// public static string RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(publickey); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// /// RSA解密 /// /// /// /// public static string RSADecrypt(string privatekey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlString(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); }
在rsa.FromXmlString()这个地方报错,那么就扩展1个读取xml的方法:
////// System.Security.Cryptography.RSA 扩展方法 /// internal static class RSAExtensions { // 处理 下面两种方式都会出现的 Operation is not supported on this platform 异常 // RSA.Create().FromXmlString(privateKey) // new RSACryptoServiceProvider().FromXmlString(privateKey) /// /// 扩展FromXmlString /// /// /// public static void FromXmlStringExtensions(this RSA rsa, string xmlString) { RSAParameters parameters = new RSAParameters(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlString); if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue")) { foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) { switch (node.Name) { case "Modulus": parameters.Modulus = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "Exponent": parameters.Exponent = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "P": parameters.P = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "Q": parameters.Q = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "DP": parameters.DP = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "DQ": parameters.DQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "InverseQ": parameters.InverseQ = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; case "D": parameters.D = (string.IsNullOrEmpty(node.InnerText) ? null : Convert.FromBase64String(node.InnerText)); break; } } } else { throw new Exception("Invalid XML RSA key."); } rsa.ImportParameters(parameters); } /// /// 扩展ToXmlString /// /// /// /// public static string ToXmlStringExtensions(this RSA rsa, bool includePrivateParameters) { RSAParameters parameters = rsa.ExportParameters(includePrivateParameters); return string.Format(" ", parameters.Modulus != null ? Convert.ToBase64String(parameters.Modulus) : null, parameters.Exponent != null ? Convert.ToBase64String(parameters.Exponent) : null, parameters.P != null ? Convert.ToBase64String(parameters.P) : null, parameters.Q != null ? Convert.ToBase64String(parameters.Q) : null, parameters.DP != null ? Convert.ToBase64String(parameters.DP) : null, parameters.DQ != null ? Convert.ToBase64String(parameters.DQ) : null, parameters.InverseQ != null ? Convert.ToBase64String(parameters.InverseQ) : null, parameters.D != null ? Convert.ToBase64String(parameters.D) : null); } } {0} {1} {2}
{3}{4} {5} {6} {7}
使用方法:
////// RSA加密 /// /// /// /// public static string RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlStringExtensions(publickey); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// /// RSA解密 /// /// /// /// public static string RSADecrypt(string privatekey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; rsa.FromXmlStringExtensions(privatekey); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); }