3

I am trying to decrypt some text that is encrypted with RSA, I have the public key to do this

`

-----BEGIN RSA PUBLIC KEY-----
MIGWAoGBAMqfGO9sPz+kxaRh/qVKsZQGul7NdG1gonSS3KPXTjtcHTFfexA4MkGA
mwKeu9XeTRFgMMxX99WmyaFvNzuxSlCFI/foCkx0TZCFZjpKFHLXryxWrkG1Bl9+
+gKTvTJ4rWk1RvnxYhm3n/Rxo2NoJM/822Oo7YBZ5rmk8NuJU4HLAhAYcJLaZFTO
sYU+aRX4RmoF
-----END RSA PUBLIC KEY-----

`

How can I load this into RSACryptoServiceProvider because this can only load from XMLString and I do not know how to convert this to Xml format

The key size is 128

I tried to initialize it using the following code

public byte[] Decrypt128(byte[] input)
{
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(128);
    rsa.ImportCspBlob(Encoding.ASCII.GetBytes(_longKey));
    return rsa.Decrypt(input, true);
}

_longKey is the content between BEGIN and END and also including the BEGIN and END, bot Bad Version of provider.

This is not a duplicate question of How do you convert Byte Array to Hexadecimal String, and vice versa?

I already know how to convert byte to hex and hex to byte, but that in any way does not help me initializing RSACryptoServiceProvider maybe give me example how that would help but at this point it doesn't

Community
  • 1
  • 1
Donald Jansen
  • 1,937
  • 4
  • 22
  • 41
  • Possible duplicate of [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – Eugene Podskal Jan 08 '17 at 09:07
  • 1
    I am not sure how this is a possible duplicate if I need to initialize `RSACryptoServiceProvider` using the public key – Donald Jansen Jan 08 '17 at 09:10
  • The contents between BEGIN and END are likely a hexademical string, or even more likely a [Base64](http://stackoverflow.com/questions/7134837/how-do-i-decode-a-base64-encoded-string) one. So Encoding.GetBytes won't work. – Eugene Podskal Jan 08 '17 at 09:13
  • According to the [MSDN Documentation on the `RSACryptoServiceProvider.ImportCspBlob`](https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.importcspblob(v=vs.110).aspx), there is no mention of anything requiring XML of any description. – Abion47 Jan 08 '17 at 09:13
  • I am going to give that a try, but it is definitely not a duplicate of that question – Donald Jansen Jan 08 '17 at 09:14
  • @Abion47 I know, but the XML I am talking about was `FromXmlString` which I tried, I then moved to `ImportCspBlob in the last 10 minutes to try something else – Donald Jansen Jan 08 '17 at 09:15
  • And [ImportCspBlob](https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.importcspblob(v=vs.110).aspx) has nothing to do with public key bytes. See http://stackoverflow.com/questions/13654400/rsacryptoserviceprovider-initialize-with-own-public-key-and-private-key for some ideas. – Eugene Podskal Jan 08 '17 at 09:16

1 Answers1

1

You could use BouncyCastle which has a PemReader allowing you to extract the modulus and exponent for the key:

using (var reader = File.OpenText("mykey.key"))
{
    var pem = new PemReader(reader);
    var o = (RsaKeyParameters)pem.ReadObject();
    using (var rsa = new RSACryptoServiceProvider())
    {
        var parameters = new RSAParameters();
        parameters.Modulus = o.Modulus.ToByteArray();
        parameters.Exponent = o.Exponent.ToByteArray();
        rsa.ImportParameters(parameters);

        // Do what you need to do with the RSACryptoServiceProvider instance
    }
}

If you don't want to have a dependency on BouncyCastle in your project, once loaded the public key into the RSACryptoServiceProvider using this method you could export it to XML for future use:

string xml = rsa.ToXmlString(false);
File.WriteAllText("mykey.xml", xml);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I think this might just work, I updated my question with the public key, I think it may be broken or something it loads perfectly but ImportParameters gives `Bad Data` – Donald Jansen Jan 08 '17 at 10:09
  • I cannot see the actual public key which is causing Bad Data in your question. – Darin Dimitrov Jan 08 '17 at 10:11
  • The Exponent of this public key is 16 bytes which greatly exceeds the 4 bytes that the `RSACryptoServiceProvider` supports. – Darin Dimitrov Jan 08 '17 at 10:23
  • Yikes, any suggestions ? – Donald Jansen Jan 08 '17 at 10:26
  • You will need another private/public key pair in which the exponent of the public key doesn't exceed 4 bytes. – Darin Dimitrov Jan 08 '17 at 10:33
  • Ish that is not possible but thanks for your help, will accept your answer this public key exists already for a long time – Donald Jansen Jan 08 '17 at 10:38
  • While `RSACryptoServiceProvider` is limited to a 4 byte exponent value, `RSACng` doesn't have that limit. It *should* work with pretty much any exponent (in excess of a 16k-bit exponent might be pushing it, though). – bartonjs Jan 09 '17 at 07:10
  • Very good point about the `RSACng` class. Unfortunately it is available from .NET 4.6, but if this is not a problem then it could be a great alternative. – Darin Dimitrov Jan 09 '17 at 09:11