xiangyuecn/RSA-csharp

Operation is not supported on this platform, .Net Core

yuessir opened this issue · 1 comments

Operation is not supported on this platform.

          var provider = RSA_PEM.FromPEM(publicKey);
            var xml = provider.ToXmlString(false);
   at System.Security.Cryptography.RSA.ToXmlString(Boolean includePrivateParameters)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\xxx\Documents\Code\ConsoleApp1\Program.cs:line 18

ToXmlString, seem to not support on .Net Core Plateform anymore.
We should implement the methods, FromXmlString and ToXmlString , I dont know why MS is not support the two methods. :={

/// <summary>Initializes an <see cref="T:System.Security.Cryptography.RSA" /> object from the key information from an XML string.</summary>
   /// <param name="xmlString">The XML string containing <see cref="T:System.Security.Cryptography.RSA" /> key information. </param>
   /// <exception cref="T:System.ArgumentNullException">The <paramref name="xmlString" /> parameter is null. </exception>
   /// <exception cref="T:System.Security.Cryptography.CryptographicException">The format of the <paramref name="xmlString" /> parameter is not valid. </exception>
   public override void FromXmlString(string xmlString)
   {
     if (xmlString == null)
       throw new ArgumentNullException(nameof (xmlString));
     RSAParameters parameters = new RSAParameters();
     SecurityElement topElement = new Parser(xmlString).GetTopElement();
     string inputBuffer1 = topElement.SearchForTextOfLocalName("Modulus");
     if (inputBuffer1 == null)
       throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object) nameof (RSA), (object) "Modulus"));
     parameters.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer1));
     string inputBuffer2 = topElement.SearchForTextOfLocalName("Exponent");
     if (inputBuffer2 == null)
       throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object) nameof (RSA), (object) "Exponent"));
     parameters.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer2));
     string inputBuffer3 = topElement.SearchForTextOfLocalName("P");
     if (inputBuffer3 != null)
       parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer3));
     string inputBuffer4 = topElement.SearchForTextOfLocalName("Q");
     if (inputBuffer4 != null)
       parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer4));
     string inputBuffer5 = topElement.SearchForTextOfLocalName("DP");
     if (inputBuffer5 != null)
       parameters.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer5));
     string inputBuffer6 = topElement.SearchForTextOfLocalName("DQ");
     if (inputBuffer6 != null)
       parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer6));
     string inputBuffer7 = topElement.SearchForTextOfLocalName("InverseQ");
     if (inputBuffer7 != null)
       parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer7));
     string inputBuffer8 = topElement.SearchForTextOfLocalName("D");
     if (inputBuffer8 != null)
       parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer8));
     this.ImportParameters(parameters);
   }

   /// <summary>Creates and returns an XML string containing the key of the current <see cref="T:System.Security.Cryptography.RSA" /> object.</summary>
   /// <returns>An XML string containing the key of the current <see cref="T:System.Security.Cryptography.RSA" /> object.</returns>
   /// <param name="includePrivateParameters">true to include a public and private RSA key; false to include only the public key. </param>
   public override string ToXmlString(bool includePrivateParameters)
   {
     RSAParameters rsaParameters = this.ExportParameters(includePrivateParameters);
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.Append("<RSAKeyValue>");
     stringBuilder.Append("<Modulus>" + Convert.ToBase64String(rsaParameters.Modulus) + "</Modulus>");
     stringBuilder.Append("<Exponent>" + Convert.ToBase64String(rsaParameters.Exponent) + "</Exponent>");
     if (includePrivateParameters)
     {
       stringBuilder.Append("<P>" + Convert.ToBase64String(rsaParameters.P) + "</P>");
       stringBuilder.Append("<Q>" + Convert.ToBase64String(rsaParameters.Q) + "</Q>");
       stringBuilder.Append("<DP>" + Convert.ToBase64String(rsaParameters.DP) + "</DP>");
       stringBuilder.Append("<DQ>" + Convert.ToBase64String(rsaParameters.DQ) + "</DQ>");
       stringBuilder.Append("<InverseQ>" + Convert.ToBase64String(rsaParameters.InverseQ) + "</InverseQ>");
       stringBuilder.Append("<D>" + Convert.ToBase64String(rsaParameters.D) + "</D>");
     }
     stringBuilder.Append("</RSAKeyValue>");
     return stringBuilder.ToString();
   }

除了.net下面见xml格式的密钥外,暂时还没有发现别的地方用到了xml格式的。他自己放弃支持了应该举双手赞成一下,我们就不要去调用xml相关的方法了。

ps:实际使用中发现那几个xml方法并没有产生任何价值,早期是用RSACryptoServiceProviderExtension完美躲开了xml格式,现在是这个仓库算是对RSACryptoServiceProviderExtension升了一下级。