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

7
  • Possible duplicate of How do you convert Byte Array to Hexadecimal String, and vice versa? Commented Jan 8, 2017 at 9:07
  • 1
    I am not sure how this is a possible duplicate if I need to initialize RSACryptoServiceProvider using the public key Commented Jan 8, 2017 at 9:10
  • The contents between BEGIN and END are likely a hexademical string, or even more likely a Base64 one. So Encoding.GetBytes won't work. Commented Jan 8, 2017 at 9:13
  • According to the MSDN Documentation on the RSACryptoServiceProvider.ImportCspBlob, there is no mention of anything requiring XML of any description. Commented Jan 8, 2017 at 9:13
  • I am going to give that a try, but it is definitely not a duplicate of that question Commented Jan 8, 2017 at 9:14

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

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
I cannot see the actual public key which is causing Bad Data in your question.
The Exponent of this public key is 16 bytes which greatly exceeds the 4 bytes that the RSACryptoServiceProvider supports.
Yikes, any suggestions ?
You will need another private/public key pair in which the exponent of the public key doesn't exceed 4 bytes.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.