简体   繁体   English

相当于openssl_private_decrypt和openssl_pkey_get_private的ASP.Net

[英]ASP.Net equivalent of openssl_private_decrypt and openssl_pkey_get_private

I want to decrypt text in ASP.Net. 我想解密ASP.Net中的文本。

The working code to decrypt the text in PHP is: 在PHP中解密文本的有效代码为:

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. 这篇文章(16319559)与我提出的要求相反,但这也不是很清楚。

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? 等效的C#方法是什么?

UPDATE: 更新:

I created a method using a couple of answers. 我创建了一个使用几个答案的方法。

Decode string-based key using PEM function, see here . 使用PEM功能解码基于字符串的密钥,请参见此处

OpenSSL code, DecodeRsaPrivateKey() and GetIntegerSize(), see here . OpenSSL代码,DecodeRsaPrivateKey()和GetIntegerSize(),请参见此处

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);

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. 有关最终答案,请参见我的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM