OpenSSL PHP RSA 加密:使用哪个库在客户端解密?

发布于 2025-01-02 22:38:46 字数 376 浏览 1 评论 0原文

我开发了一个桌面应用程序(.NET 2.0),它需要从运行 php/mysql 的服务器获取一些信息。服务器端的提供者决定使用 openssl_private_encrypt 方法进行加密,并为我提供了一个 publickey.pem 文件。我不擅长 php/mysql,但我知道如果我使用 asp .net,情况不会有任何不同。

我的问题是如何解密 .NET 2.0 应用程序中的私有加密数据。我所有使用 RSACrypoServiceProvider 和公钥的尝试都被证明是不成功的。

我偶然发现了 ManagedOpenSSL 库,但这也不好。

我确信 StackOverflow 的许多人都会做类似的事情,如果知道他们是如何做到的那就太好了。

提前感谢您的所有帮助。

I have developed a desktop application (.NET 2.0) and it needs to get some information from a server running php/mysql. The provider at server end has decided to use encryption using openssl_private_encrypt method and has provided me a publickey.pem file. I am not good at php/mysql but i do know that it would not be any different if i were using asp .net.

My question is how do i decrypt private-enrypted data in a .NET 2.0 application. All my attempts to use the RSACrypoServiceProvider with the publickey have proven unsuccessful.

I stumbled upon ManagedOpenSSL library but that was no good either.

I am certain many at StackOverflow would have done similar stuff and it would be nice o know how they did it.

Appreciate all your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2025-01-09 22:38:46

.NET 使用 OpenSSL 不支持的 XML 格式的密钥。我的建议是使用 phpseclib(一种纯 PHP RSA 实现)生成密钥对 - 或者至少将现有密钥对转换为 XML

.NET uses XML formatted keys which OpenSSL doesn't support. My recommendation would be to generate the key pair - or at least convert the existing one to XML - with phpseclib, a pure PHP RSA implementation.

南巷近海 2025-01-09 22:38:46

这是我用来让它工作的!

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            string responseFromServerEnc = string.Empty;

            byte[] data = null;

            if (HttpStatusCode.OK == response.StatusCode)
            {
                MemoryStream memoryStream = new MemoryStream(0x10000);

                using (Stream responseStream =            request.GetResponse().GetResponseStream())
                {
                    byte[] buffer = new byte[0x1000];
                    int bytes;
                    while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        memoryStream.Write(buffer, 0, bytes);
                    }

                    data = memoryStream.ToArray();
                }

                response.Close();
            }


            const string pubKey = "-----BEGIN PUBLIC KEY-----\n key from .pem file \n-----END PUBLIC KEY-----";
            PemReader pem = new PemReader(new StringReader(pubKey));
            RsaKeyParameters bcp = (RsaKeyParameters)pem.ReadObject();

            string responseFromServer = Encoding.UTF8.GetString(Decrypt(data,bcp));

responseFromServerEnc 是加密消息,responseFromServer 已正确解密。

Here is what i used to make it work!

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();
            string responseFromServerEnc = string.Empty;

            byte[] data = null;

            if (HttpStatusCode.OK == response.StatusCode)
            {
                MemoryStream memoryStream = new MemoryStream(0x10000);

                using (Stream responseStream =            request.GetResponse().GetResponseStream())
                {
                    byte[] buffer = new byte[0x1000];
                    int bytes;
                    while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        memoryStream.Write(buffer, 0, bytes);
                    }

                    data = memoryStream.ToArray();
                }

                response.Close();
            }


            const string pubKey = "-----BEGIN PUBLIC KEY-----\n key from .pem file \n-----END PUBLIC KEY-----";
            PemReader pem = new PemReader(new StringReader(pubKey));
            RsaKeyParameters bcp = (RsaKeyParameters)pem.ReadObject();

            string responseFromServer = Encoding.UTF8.GetString(Decrypt(data,bcp));

And responseFromServerEnc was the encrypted message and responseFromServer was correctly decrypted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文