使用 x509 证书的 rsacryptoserviceprovider C#

发布于 2024-08-09 02:20:40 字数 716 浏览 6 评论 0原文

我正在使用 makecert 生成的证书,它同时具有私钥和公钥。 java端使用这个公钥来加密数据,然后.net将其解密回来。

我正在尝试解密 Java 的加密 64 位编码字符串并获取错误数据。

为了查看网络端是否一切正常,我首先尝试使用公钥进行加密,然后使用相同的证书使用私钥进行解密。我的代码看起来像这样。

X509Certificate2 cert = GetCert(key, StoreName.My, StoreLocation.LocalMachine);
RSACryptoServiceProvider provider =  (RSACryptoServiceProvider)cert.PrivateKey;

RSACryptoServiceProvider publicprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;

if (cert.HasPrivateKey)
    MessageBox.Show("Got private key");

byte[] encrypted = publicprovider.Encrypt(Encoding.UTF8.GetBytes(text), false);
byte[] decryptedBytes = provider.Decrypt(encrypted, false); 

即使在这里我也遇到错误。我错过了什么吗?

该证书看起来对于公钥和私钥都有效。

i am using a certificate generated by makecert which has both private and public key.
The java side uses this public key to encrypt the data and .net decrypts it back.

I am trying to decrypt Java's encrypted 64 bit encoded string and getting bad data.

To see if all is good on.Net end, I frist tried to encrypt with the public key and then decrypt with private using the same certificate. My code looks like this.

X509Certificate2 cert = GetCert(key, StoreName.My, StoreLocation.LocalMachine);
RSACryptoServiceProvider provider =  (RSACryptoServiceProvider)cert.PrivateKey;

RSACryptoServiceProvider publicprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;

if (cert.HasPrivateKey)
    MessageBox.Show("Got private key");

byte[] encrypted = publicprovider.Encrypt(Encoding.UTF8.GetBytes(text), false);
byte[] decryptedBytes = provider.Decrypt(encrypted, false); 

Even here I am getting the error. Am i Missing something?

The certificate looks valid with both public and private key.

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

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

发布评论

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

评论(4

鹤仙姿 2024-08-16 02:20:42

以下代码对我来说效果很好:

        RSACryptoServiceProvider privateKey = new RSACryptoServiceProvider();
        privateKey.FromXmlString("<RSAKeyValue><Modulus>wL8s+C8SnnlaaqR+VsyijmxOJOARNa4o7ZNsqfy3+9J9Ol2JNSjjMfQWoUnFtClzJBlZhU5KtuazQe8ZKXTX9YvKoJdRhlsonZkC04qiTMdO/FZIH00GrCRxeQ7XDnQnvPB9Bdsvs//7zrY3f7eLIkpIyK9cQHU+5jjJd5IT0eE=</Modulus><Exponent>AQAB</Exponent><P>83xxN7jvpg5z16pxz2tIQIdqd/EfmikR9Q2TjG2tosWkUSvtyx0xHZ9EqdTUbSGZZ+jgrabzkafYc7Mplylwew==</P><Q>yqcnYSZEXHwJvRWi2V09PNEENTozQZywcFptUUGar9TciaQvoNv3lpnfzUKNBRdhzq4lImxkamajZlTWE5buUw==</Q><DP>37HqilkbwyHwB6mOGhPkM3S1ujAK6qTk3JB2iEOTjMGrru9+7maJYz+Z47Wm3ARMXgyzrpZ9m8nqsJFfmoL11Q==</DP><DQ>v285tv8kMs2FkZYfuP/oOkwkkneBNejjj68Md2bmzlThZDCyQV2pvB1tmgPVHUsiPNCrCaKlFRISJzfa5rR8Ow==</DQ><InverseQ>fgJE2TRe/SS+YqW0/I+FtHrdfbbao0/R3pHD4r4oceZQUemlBgZ7DxOAetebHKthlOdjGkmfWYB8EU4XoWggqw==</InverseQ><D>FMLCwjy3wbAKiCANp6XFAJgz1o7365NFv0k41BpvasViTa4TgFFWH2ROJ7M9g0lPqJy+YrhrHcY9mqV5TVjTheQp0JeckrgO2B39XngPMAMMdne3rWGpf0Pfbj3FLfchMk6XYDXSZzCS2CmSeRA4aBMb+4R3YurixyJLrnGRMH0=</D></RSAKeyValue>");
        RSACryptoServiceProvider publicKey = new RSACryptoServiceProvider();
        publicKey.FromXmlString("<RSAKeyValue><Modulus>wL8s+C8SnnlaaqR+VsyijmxOJOARNa4o7ZNsqfy3+9J9Ol2JNSjjMfQWoUnFtClzJBlZhU5KtuazQe8ZKXTX9YvKoJdRhlsonZkC04qiTMdO/FZIH00GrCRxeQ7XDnQnvPB9Bdsvs//7zrY3f7eLIkpIyK9cQHU+5jjJd5IT0eE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");

        {
            string text = "foo";
            byte[] encrypted = publicKey.Encrypt(Encoding.UTF8.GetBytes(text), false);
            byte[] decryptedBytes = privateKey.Decrypt(encrypted, false);
        }

您能否仔细检查导出的私钥是否来自 cert.PrivateKey,公钥是否来自 cert.PublicKey.Key?

The following code works fine for me:

        RSACryptoServiceProvider privateKey = new RSACryptoServiceProvider();
        privateKey.FromXmlString("<RSAKeyValue><Modulus>wL8s+C8SnnlaaqR+VsyijmxOJOARNa4o7ZNsqfy3+9J9Ol2JNSjjMfQWoUnFtClzJBlZhU5KtuazQe8ZKXTX9YvKoJdRhlsonZkC04qiTMdO/FZIH00GrCRxeQ7XDnQnvPB9Bdsvs//7zrY3f7eLIkpIyK9cQHU+5jjJd5IT0eE=</Modulus><Exponent>AQAB</Exponent><P>83xxN7jvpg5z16pxz2tIQIdqd/EfmikR9Q2TjG2tosWkUSvtyx0xHZ9EqdTUbSGZZ+jgrabzkafYc7Mplylwew==</P><Q>yqcnYSZEXHwJvRWi2V09PNEENTozQZywcFptUUGar9TciaQvoNv3lpnfzUKNBRdhzq4lImxkamajZlTWE5buUw==</Q><DP>37HqilkbwyHwB6mOGhPkM3S1ujAK6qTk3JB2iEOTjMGrru9+7maJYz+Z47Wm3ARMXgyzrpZ9m8nqsJFfmoL11Q==</DP><DQ>v285tv8kMs2FkZYfuP/oOkwkkneBNejjj68Md2bmzlThZDCyQV2pvB1tmgPVHUsiPNCrCaKlFRISJzfa5rR8Ow==</DQ><InverseQ>fgJE2TRe/SS+YqW0/I+FtHrdfbbao0/R3pHD4r4oceZQUemlBgZ7DxOAetebHKthlOdjGkmfWYB8EU4XoWggqw==</InverseQ><D>FMLCwjy3wbAKiCANp6XFAJgz1o7365NFv0k41BpvasViTa4TgFFWH2ROJ7M9g0lPqJy+YrhrHcY9mqV5TVjTheQp0JeckrgO2B39XngPMAMMdne3rWGpf0Pfbj3FLfchMk6XYDXSZzCS2CmSeRA4aBMb+4R3YurixyJLrnGRMH0=</D></RSAKeyValue>");
        RSACryptoServiceProvider publicKey = new RSACryptoServiceProvider();
        publicKey.FromXmlString("<RSAKeyValue><Modulus>wL8s+C8SnnlaaqR+VsyijmxOJOARNa4o7ZNsqfy3+9J9Ol2JNSjjMfQWoUnFtClzJBlZhU5KtuazQe8ZKXTX9YvKoJdRhlsonZkC04qiTMdO/FZIH00GrCRxeQ7XDnQnvPB9Bdsvs//7zrY3f7eLIkpIyK9cQHU+5jjJd5IT0eE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>");

        {
            string text = "foo";
            byte[] encrypted = publicKey.Encrypt(Encoding.UTF8.GetBytes(text), false);
            byte[] decryptedBytes = privateKey.Decrypt(encrypted, false);
        }

Can you double-check that the exported private key is from cert.PrivateKey and the public key is from cert.PublicKey.Key?

乙白 2024-08-16 02:20:42

当我尝试使用 c# 查找 x509 证书和 rsa 的 makcert 用法示例时,我偶然发现了此页面,不幸的是它只提供了部分解决方案。我将所有内容放在人们可能感兴趣的博客条目中,可以在这里找到:
http://nick-howard.blogspot.com /2011/05/makecert-x509-certificates-and-rsa.html

I stumbled across this page when I was trying to find examples of makcert usage with x509 certificates and rsa using c#, and unfortunately it only provided part of the solution. I put all the bits together in a blog entry that people might be interested in, and it can be found here:
http://nick-howard.blogspot.com/2011/05/makecert-x509-certificates-and-rsa.html

‘画卷フ 2024-08-16 02:20:42

我终于找到了问题所在。我没有将密钥放入 makecert 以将其定义为 RSA 加密密钥。

I finally found the problem. I wasn't putting the key to makecert to define it as RSA Crypto key.

苄①跕圉湢 2024-08-16 02:20:40

我对自签名证书有同样的问题,问题是我使用开关 -sky 签名 而不是 -sky 交换 生成证书(您使用签名用于签名和交换加密/解密)

这是我有效的 makecert 的完整命令:

makecert -r -pe -a sha1 -n "CN=MyName" -ss my -sr CurrentUser -sky exchange

I had the same problem with a self-signed cert, the issue was that I was generating the cert with the switch -sky signature instead of -sky exchange (you use signature for signing and exchange for encryption/decryption)

Here is my full command to makecert that works:

makecert -r -pe -a sha1 -n "CN=MyName" -ss my -sr CurrentUser -sky exchange
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文