0

I have this Java code to create a public key based on base64 byte array in Java:

return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(base64PublicKeyBytes));

That line is subsequently used to encrypt some data.

Assume that I have access to the base64 encoded public key string.
Would the equivalent C# code to generate the key be as simple as:

base64PublicKeyBytes =  Convert.FromBase64String(base64PublicKey);

and this can then be used with RSACryptoServiceProvider as follows:

var rsa = new RSACryptoServiceProvider(2048);
                    var rsaKeyInfo = rsa.ExportParameters(false);
                    rsaKeyInfo.Modulus = base64PublicKeyBytes;
Klaus Nji
  • 18,107
  • 29
  • 105
  • 185
  • No, it would not be that simple. .NET does not have support for that format of public key, also known as SubjectPublicKeyInfo format. There are a few ways to deal with this. One is to use the Bouncycastle C# library which does support this format. Another is to convert it to a format .NET supports, like the XML format of `RSA.ToXMLString()` – President James K. Polk Oct 10 '18 at 11:32
  • Thanks for the response. I figured it was not as straight forward. Here is what I am trying: #if WINDOWS var existingPublicKeyData = rsa.ToXmlString(false); // extract the original exponent and re-use var exponent = ParseExponentFromPublicKeyInfo(existingPublicKeyData); var keyInfo = CreateRSARequiredKeyFormatCreateRSARequiredKeyFormat(exponent, orgEncryptionKey); Console.WriteLine("Re-import key information. "); rsa.FromXmlString(keyInfo); #endif – Klaus Nji Oct 10 '18 at 15:07

0 Answers0