【问题标题】:RSA decrypt with certificat and GetRSAPublicKey : publickey doesn't exist使用证书和 GetRSAPublicKey 进行 RSA 解密:公钥不存在
【发布时间】:2020-01-11 11:07:56
【问题描述】:

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

我在这里看到Best way to initiate RSACryptoServiceProvider from x509Certificate2?

“从 .NET 4.6 开始,不再建议按照 RSACryptoServiceProvider 的建议进行强制转换。现在这更是一个问题,因为有多个版本的 .NET(例如 .NET Core)。

通过这种方式转换为 RSACryptoServiceProvider,您很有可能会遇到此转换异常(取决于所使用的平台和库):

Unable to cast object of type 'System.Security.Cryptography.RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'

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

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

     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

为什么 GetRSAPublicKey() 似乎没有公钥? 是不是最好的解密方式?

【问题讨论】:

    标签: c# rsa x509certificate2


    【解决方案1】:

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

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

    【讨论】:

    • 从客户端我需要使用服务器公钥来加密。然后我将加密的消息发送到服务器,服务器可以用他的私钥解密。我认为加密功能的公钥或私钥并不重要,对他来说这只是一个密钥。
    • 好吧...那你为什么要打电话给 Decrypt 呢? :)
    • 因为当服务器向我发送一些东西时,服务器用他的私钥加密,所以我需要用他的公钥解密。我需要做加密和解密
    • @florent 我非常怀疑服务器使用他们的私钥加密了任何东西,如果他们这样做了,那么您就无法解决 .NET 中内置加密类型的问题。如果他们签署了数据,您可以使用公钥对其进行验证,但加密是公钥,解密是私钥。应用加密填充然后使用私钥转换数据在数学上是可行的,但非常不标准。 “未找到密钥”错误是您正在使用公钥进行解密(私钥操作)(私钥部分是“未找到”)。
    • 你说:“加密是公钥,解密是私钥。”我不认为这是我想做的那么具体。使用 BouncyCastle 是可能的:var decryptEngine = new Pkcs1Encoding(new Org.BouncyCastle.Crypto.Engines.RsaEngine()); decryptEngine.Init(false, cert.GetPublicKey()); string decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesCypherText, 0, bytesCypherText.Length)); 例如在 php 中有:openssl_public_encrypt / openssl_private_decrypt(). and openssl_private_encrypt / openssl_public_decrypt(). 在 rsa 类中真的不可能吗?
    猜你喜欢
    • 2012-05-07
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2011-08-16
    • 2021-10-03
    • 2014-09-03
    • 1970-01-01
    相关资源
    最近更新 更多