2

I have to convert below dotnet framework code to generate RSA public and private keys in dotnet core.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
string publicKey = RSA.ToXmlString(false);
string privateKey = RSA.ToXmlString(true);

ABove Code is not supported in Dotnet Core and throw below error:

Exception has occurred: CLR/System.PlatformNotSupportedException An unhandled exception of type 'System.PlatformNotSupportedException' occurred in System.Security.Cryptography.Algorithms.dll: 'Operation is not supported on this platform.' at System.Security.Cryptography.RSA.ToXmlString(Boolean includePrivateParameters)

Solution:Below extension worked for me

public static class RSACryptoServiceProviderExtensions
    {
         public static void ToXmlFile(this RSA rsa, bool includePrivateParameters, string xmlFilePath)  
        {  
            RSAParameters parameters = rsa.ExportParameters(includePrivateParameters);  
            File.WriteAllText(xmlFilePath,  
                string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",  
                  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)  
                  );  
        }  
    }
Mayank
  • 337
  • 2
  • 9
  • 22

2 Answers2

2

RSACryptoServiceProvider is not recommended. I think you can still use it with package System.Security.Cryptography.Algorithms installed.

Another way is to use RSA base class, refering to implement RSA in .NET core:

.NET Core

using (RSA rsa = RSA.Create())
{
    rsa.KeySize = desiredKeySizeInBits;

    // when the key next gets used it will be created at that keysize.
    DoStuffWithThePrivateKey(rsa);
}
ALFA
  • 1,726
  • 1
  • 10
  • 19
0

If you need to do with RSACryptoServiceProvider. You may try with below code in .NET Core.

.NET Core

public static string GeneratePEM(RSA rsa, bool includePrivateParameters = false)
    {
        byte[] keyBytes = includePrivateParameters ? rsa.ExportRSAPrivateKey() : rsa.ExportRSAPublicKey();
        var builder = new StringBuilder($"-----BEGIN RSA {(includePrivateParameters ? "PRIVATE" : "PUBLIC")} KEY");
        builder.AppendLine("-----");

        var base64String = Convert.ToBase64String(keyBytes);
        var offset = 0;
        const int LINE_LENGTH = 64;

        while (offset < base64String.Length)
        {
            var lineEnd = Math.Min(offset + LINE_LENGTH, base64String.Length);
            builder.AppendLine(base64String.Substring(offset, lineEnd - offset));
            offset = lineEnd;
        }

        builder.Append($"-----END RSA {(includePrivateParameters ? "PRIVATE" : "PUBLIC")} KEY");
        builder.AppendLine("-----");
        return builder.ToString();
    }
Yash Mochi
  • 769
  • 6
  • 15