2

I want to decrypt text in ASP.Net.

The working code to decrypt the text in PHP is:

function rsaDecryption($dataEncrypted, &$dataDecrypted)
{
    // Decrypt argument
    $key = openssl_pkey_get_private(RsaKeyPrivate, RsaKeyPassphrase);
    if (FALSE == $key)
    {
        echo("Failed to get the private key<br />\n");
        return false;
    }
    if(!openssl_private_decrypt($dataEncrypted, $dataDecrypted, $key))
    {
        echo("Failed to decrypt message.<br />\n");
        return false;
    }

    return TRUE;
}

I saw several articles on the web, but none was clear to me. This article (16319559) is the opposite than what I asked, but that was not really clear either.

The private key looks something like this

String myPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n<whole bunch of characters>==\n-----END RSA PRIVATE KEY-----

What are the equivalent C# methods?

UPDATE:

I created a method using a couple of answers.

Decode string-based key using PEM function, see here.

OpenSSL code, DecodeRsaPrivateKey() and GetIntegerSize(), see here.

I also needed the following:

String strArgHtmlDecoded = HttpUtility.UrlDecode(dataEncrypted);
Byte[] byArgNo64 = Convert.FromBase64String(strArgHtmlDecoded);

That results in

Byte[] byArgDecrypted = oProviderRsa.Decrypt(byArgNo64, false);
strDecrypted = Encoding.Default.GetString(byArgDecrypted);
Community
  • 1
  • 1
Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130

1 Answers1

3

I figured it out.

The answer was a collection of code from various places.

The link in the comment was part of it. I saw that same article once before, but without the other code, I did not see the relevance.

See my update for the final answer.

Sarah Weinberger
  • 15,041
  • 25
  • 83
  • 130
  • 1
    You should probably put answers in answer blocks, and then accept the answer. That makes it easier on future visitors who learned to use the site that way. – jww Apr 01 '14 at 00:30