C#RSA解密参数不正确 [英] C# RSA decrypt parameter is incorrect

查看:208
本文介绍了C#RSA解密参数不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解密来自使用SSL证书保护的Web套接字的通信.证书已安装在计算机上,并且存在私钥.我有这个解密功能

I am trying to decrypt communication from web sockets secured with SSL certificate. The certificate is installed on the machine and the private key is present. I have this decrypt function

public static string DecryptTry(X509Certificate2 x509, string stringTodecrypt)
{
        try
        {
            if (x509 == null || string.IsNullOrEmpty(stringTodecrypt))
                throw new Exception("A x509 certificate and string for decryption must be provided");

            if (!x509.HasPrivateKey)
                throw new Exception("x509 certificate does not contain a private key for decryption");

            if(x509 == null)
            {
                Console.WriteLine("certificate is null");
                return "";
            }

            Console.WriteLine("decoding text with private key " + x509.HasPrivateKey);//true

            using (RSA csp = (RSA)x509.PrivateKey)
            {
                byte[] bytestoDecrypt = Encoding.UTF8.GetBytes(stringTodecrypt).Take(256).ToArray();
                Console.WriteLine("key size: " + x509.PrivateKey.KeySize + " received data length: " + stringTodecrypt.Length + " bytes length: " + bytestoDecrypt.Length);
                Console.WriteLine(Encoding.UTF8.GetString(bytestoDecrypt));
                byte[] bytesDecrypted = csp.Decrypt(bytestoDecrypt, RSAEncryptionPadding.OaepSHA256); //ERROR HERE

                return Encoding.UTF8.GetString(bytesDecrypted);
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Error while decrypting text: " + e.Message + e.HResult +  e.StackTrace);
            return "";
        }
    }

但是 csp.Decrypt 抛出错误

参数不正确

我已经尝试了所有填充参数-似乎没有任何改变.有人知道问题可能在哪里吗?我想念什么吗?**

I have tried all padding parameters - none of the seemed to make a difference. Does anybody know where the problem might be? Am I missing something? **

**要添加更多背景信息:WebSocket客户端正在侦听的网站是安全的HTTPS,CA对该SSL证书进行了签名,并拥有我对其所有信息的完全访问权限.最初的问题是用于加密的WebSocket通信的握手.我当时以为我可以用私钥解密它,这就是问题所在.传入请求(或握手)的长度在490到520字节之间,这就是 .Take(256)的原因.我当时正在考虑将文本分成多个部分,分别对其进行解码,然后再组合在一起.但是,那把我带到了这里.最后一个想法:这是一个.NET控制台应用程序.可以通过将其转换为IIS接受的格式来解决该问题吗?机器上的IIS已安装了证书...可能会有所作为吗?预先感谢!

** To add some more background info: The website where the WebSocket client is listening is secured HTTPS, the SSL certificate is signed by CA with my full access to all of its information. The initial problem is handshake for the WebSocket communication which comes encrypted. I was thinking I would be able to decrypt it with the private key and that is where the problem occurs. The length of the incoming request (or handshake) is between 490 and 520 bytes, so that is the reason for .Take(256). I was thinking to split the text into multiple, decode them separately and put together after. That, however, brought me here. One final thought: This is a .NET console application. Could the problem be possibly fixed by converting it to a format that IIS accepts? The IIS on the machine has the certificate installed... could it possibly make a difference? Thanks in advance!

推荐答案

我不确定您要实现什么:

I'm not sure what you want to achieve:

  • 使用RSA解密内容
  • 解密HTTP的TLS层
  • 解密Websocket协议中的内容

我认为您应该提供更多有关其工作方式的信息.您能否显示函数功能的示例输入,意思是:

I think that you should give some more informations how it works. Can you show example input for function function, means:

DecryptTry(X509Certificate2 x509,字符串stringTodecrypt){...}

"strindTodecypt"的外观如何?你在那放什么您如何连接到该服务器以通过TLS解密数据?

How 'strindTodecypt' looks like? What do you put there? How do you connect to this server to get data to decrypt, via TLS?

这篇关于C#RSA解密参数不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆