本文介绍了rsacryptoserviceprovider使用x509证书c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用由makecert生成的具有私钥和公钥的证书。
java端使用这个公钥加密数据,而.net解密它。



我试图解密Java的加密64位编码字符串,坏数据。



要查看是否所有都是好的在.net结束,我frist试图加密与公共密钥,然后使用相同的证书私人解密。我的代码如下所示。

  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 [] decryptptedBytes = provider.Decrypt(encrypted,false);

即使在这里,我得到的错误。

解决方案



证书看起来对公钥和私钥都有效。 / div>

我终于找到了问题。我没有把钥匙makecert定义为RSA加密密钥。


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.

解决方案

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

这篇关于rsacryptoserviceprovider使用x509证书c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 12:45