問題描述
我正在使用由 makecert 生成的證書,該證書具有私鑰和公鑰.Java 端使用此公鑰加密數據,.net 將其解密.
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.
我正在嘗試解密 Java 的加密 64 位編碼字符串并獲取錯誤數據.
I am trying to decrypt Java's encrypted 64 bit encoded string and getting bad data.
為了查看.Net 端是否一切正常,我首先嘗試使用公鑰加密,然后使用相同的證書使用私鑰解密.我的代碼如下所示.
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.
推薦答案
我終于找到了問題所在.我沒有把 makecert 的密鑰定義為 RSA 加密密鑰.
I finally found the problem. I wasn't putting the key to makecert to define it as RSA Crypto key.
這篇關于rsacryptoserviceprovider 使用 x509 證書 c#的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!