使用 x509 证书的 rsacryptoserviceprovider C#
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码对我来说效果很好:
您能否仔细检查导出的私钥是否来自 cert.PrivateKey,公钥是否来自 cert.PublicKey.Key?
The following code works fine for me:
Can you double-check that the exported private key is from cert.PrivateKey and the public key is from cert.PublicKey.Key?
当我尝试使用 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
我终于找到了问题所在。我没有将密钥放入 makecert 以将其定义为 RSA 加密密钥。
I finally found the problem. I wasn't putting the key to makecert to define it as RSA Crypto key.
我对自签名证书有同样的问题,问题是我使用开关
-sky 签名
而不是-sky 交换
生成证书(您使用签名用于签名和交换加密/解密)这是我有效的 makecert 的完整命令:
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: