问题描述
我使用由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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!