C# RSA使用Certificate和GetRSAPublicKey解密:publickey不';不存在

C# RSA使用Certificate和GetRSAPublicKey解密:publickey不';不存在,c#,rsa,x509certificate2,C#,Rsa,X509certificate2,我想从我的证书中检索公钥,然后解密我的消息。 我使用X509Certificate2从我的证书中获取信息,然后使用RSA类进行解密,但我有一个错误:密钥不存在 我在这里看到: “自.NET 4.6以来,不再推荐按照建议向RSACryptServiceProvider强制转换。由于.NET有多个版本(如.NET Core),这一问题现在更为严重。” 通过以这种方式强制转换到RSACryptServiceProvider,您很有可能获得此强制转换异常(取决于所使用的平台和库): 原因是在Window

我想从我的证书中检索公钥,然后解密我的消息。 我使用X509Certificate2从我的证书中获取信息,然后使用RSA类进行解密,但我有一个错误:密钥不存在

我在这里看到:

“自.NET 4.6以来,不再推荐按照建议向RSACryptServiceProvider强制转换。由于.NET有多个版本(如.NET Core),这一问题现在更为严重。”

通过以这种方式强制转换到RSACryptServiceProvider,您很有可能获得此强制转换异常(取决于所使用的平台和库):

原因是在Windows上使用的每个平台的实际实现可能不同。”

所以我尝试使用RSA类而不是RSACryptServiceProvider

     string pemcert;

     X509Certificate2 Lcertificate = new X509Certificate2(Encoding.UTF8.GetBytes(pemcert));

     //my message
     byte[] bytesCypherText = Convert.FromBase64String(b64_crypted_text);

     using (RSA LRSApublicKeyProvider = Lcertificate.GetRSAPublicKey())
     {

        string decrypted = Encoding.UTF8.GetString((LRSApublicKeyProvider.Decrypt(bytesCypherText, RSAEncryptionPadding.Pkcs1)));
        return decrypted;
     }
我有一个错误:

     Certificat: Error => key doesn't exist
       /  System.Security.Cryptography.CryptographicException

       Certificat. StackTrace :    à System.Security.Cryptography.NCryptNative.DecryptData[T](SafeNCryptKeyHandle key, Byte[] data, T& paddingInfo, AsymmetricPaddingMode paddingMode, NCryptDecryptor`1 decryptor)
        à System.Security.Cryptography.NCryptNative.DecryptDataPkcs1(SafeNCryptKeyHandle key, Byte[] data)
        à System.Security.Cryptography.RSACng.Decrypt(Byte[] data, RSAEncryptionPadding padding)
        à EA_Crocodile.CryptoHelper.RSADecryptJEAM(String b64_crypted_text, String pemcert) 
如果我这样做:

     Lcertificate.GetPublicKeyString() I have the key: 3082020A0282020100B39A5....
     Lcertificate.GetRSAPublicKey().ToString(): System.Security.Cryptography.RSACng
如果我这样做:

     Lcertificate.GetPublicKeyString() I have the key: 3082020A0282020100B39A5....
     Lcertificate.GetRSAPublicKey().ToString(): System.Security.Cryptography.RSACng
为什么GetRSAPublicKey()似乎没有公钥?
这是解密的最佳方式吗?

RSA公钥只能用于加密或签名验证


要进行解密,您需要私钥(GetRSAPrivateKey,它要求证书知道在哪里可以找到它)。

从客户端,我需要使用服务器公钥进行加密。然后我将加密的消息发送到服务器,服务器可以用他的私钥解密。我觉得函数加密的公钥或私钥都不重要,对他来说,这只是一个密钥。好吧。。。那你为什么叫Decrypt呢?:)因为当服务器发送给我东西时,服务器用他的私钥加密,所以我需要用他的公钥解密。我需要做加密和加密decrypt@florent我非常怀疑服务器用他们的私钥加密了任何东西,如果他们加密了,你就没有办法用.NET中内置的加密类型来解决你的问题。若他们签署了数据,你们可以用公钥验证,但加密是公钥,解密是私钥。应用加密填充,然后使用私钥转换数据在数学上是可能的,但非常不标准。“找不到密钥”错误是,您正在使用公钥进行解密(私钥操作)(私钥的一半是“找不到的”)。您说:“加密是公钥,解密是私钥。”我不认为这是我想要做的那么具体。使用BouncyCastle可以:
var decryptEngine=new pkcs1encode(new Org.BouncyCastle.Crypto.Engines.RsaEngine());decryptEngine.Init(false,cert.GetPublicKey());string decrypted=Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesCypherText,0,bytesCypherText.Length))
例如,在php中有:
openssl\u public\u encrypt/openssl\u private\u decrypt()。还有openssl_private_encrypt/openssl_public_decrypt()。
在rsa类中真的不可能吗?