CryptographicException:未知错误“80007005”。在 .Net Compact Framework 中调用 RSACryptoServiceProvider.Decrypt() 时

发布于 2024-09-03 15:02:55 字数 721 浏览 7 评论 0 原文

我正在尝试使用 RSACryptoServiceProvider 进行加密/解密。加密工作正常,但 Decrypt 方法会引发异常并显示消息:

未知错误“80007005”。

这是代码:

Byte[] plainData = encoding.GetBytes(plainText);
Byte[] encryptedData;
RSAParameters rsap1;
Byte[] decryptedData;
using (RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider())
{
   encryptedData = rsa1.Encrypt(plainData, false);
   rsap1 = rsa1.ExportParameters(false);
}

using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider())
{
   rsa2.ImportParameters(rsap1);
   decryptedData = rsa2.Decrypt(encryptedData, false);
}

decryptedText = encoding.GetString(decryptedData, 0, decryptedData.Length);

有人知道解决方法吗?

谢谢!

I am trying to use the RSACryptoServiceProvider to encrypt/decrypt. Encrypting works fine, but the Decrypt method throws an exception with the message:

Unknown Error '80007005'.

This is the code:

Byte[] plainData = encoding.GetBytes(plainText);
Byte[] encryptedData;
RSAParameters rsap1;
Byte[] decryptedData;
using (RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider())
{
   encryptedData = rsa1.Encrypt(plainData, false);
   rsap1 = rsa1.ExportParameters(false);
}

using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider())
{
   rsa2.ImportParameters(rsap1);
   decryptedData = rsa2.Decrypt(encryptedData, false);
}

decryptedText = encoding.GetString(decryptedData, 0, decryptedData.Length);

Is anyone aware of a workaround?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗藏城府 2024-09-10 15:02:55

修复了代码!我想我毕竟不需要指定容器......

Byte[] plainData = encoding.GetBytes(plainText);
Byte[] encryptedData;
Byte[] decryptedData;
using (RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider())
{
    RSAParameters rsap1 = rsa1.ExportParameters(false);

    using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider())
    {
        rsa2.ImportParameters(rsap1);
        encryptedData = rsa2.Encrypt(plainData, false);
    }

    decryptedData = rsa1.Decrypt(encryptedData, false);
}

decryptedText = encoding.GetString(decryptedData, 0, decryptedData.Length);

Fixed the code! I guess I do not need to specify a container after all...

Byte[] plainData = encoding.GetBytes(plainText);
Byte[] encryptedData;
Byte[] decryptedData;
using (RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider())
{
    RSAParameters rsap1 = rsa1.ExportParameters(false);

    using (RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider())
    {
        rsa2.ImportParameters(rsap1);
        encryptedData = rsa2.Encrypt(plainData, false);
    }

    decryptedData = rsa1.Decrypt(encryptedData, false);
}

decryptedText = encoding.GetString(decryptedData, 0, decryptedData.Length);
梦巷 2024-09-10 15:02:55
rsap1 = rsa1.ExportParameters(false);

通过向此方法传递 false,您将选择不导出私钥。如果没有私钥,将很难解密数据。尝试将 true 传递给导出方法。

rsap1 = rsa1.ExportParameters(false);

By passing false to this method, you're choosing to not export the private key. Without the private key it will be difficult to decrypt the data. Try passing true to the export method.

听不够的曲调 2024-09-10 15:02:55

使用 RSA 时,您需要了解密钥管理的基础知识。您没有指定加密期间使用什么密钥容器。您期望使用什么密钥?默认用户密钥?机器钥匙?您了解默认用户密钥和机器密钥是什么吗?更不用说为什么用 RSA 加密任何东西这个明显的问题了? RSA 加密用于加密会话密钥,并且有专门的密钥交换协议可以处理这种开箱即用的问题(面向流,如 TLS 或面向文档,如 S/MIME)。您应该使用这些开箱即用的协议之一,而不是推出自己的加密方案。你将会搞砸密钥管理,这是肯定的。

当您尝试解密时,解密者是否拥有与加密期间使用的公钥相对应的私钥?

请参阅:

请注意,这些是只是 MSDN 中的简单代码示例,如果没有对密码学(特别是密钥管理)有非常深入的了解,任何人都不应使用。

我建议您考虑使用高级类,例如 SslStream 用于加密数据交换。对于文档存储加密方案,您最好使用操作系统设施或依赖 ProtectedData 类。再次强调,除非您真的知道自己在做什么,否则不要自行加密(在这种情况下您就不会在这里提出问题)。

When using RSA you need to understand the basics of key management. You did not specify what key container to use during encryption. What key do you expect to be used? The default user key? The machine key? Do you understand what the default user key and the machine keys are ? Not to mention the obvious question of why do you encrypt anything with RSA? RSA encryption is used solely for encrypting session keys, and there are dedicated key exchange protocols that take care of this out-of-the-box (stream oriented like TLS or document oriented like S/MIME). You should use one of these out-of-the-box protocols and not roll your own encryption scheme. You will screw up key management, that is guaranteed.

When you attempt to decrypt, does the decryptor has possession of the private key corresponding to the public key used during encryption?

See:

Note that these are just simple code samples in MSDN and should never be used by anyone without a very deep understanding of cryptography, and specially key management.

I recommend you look into using a high level class like SslStream for encrypting data exchanges. For a document storage encryption scheme you better use the OS facilities or rely on ProtectedData class. Again, do not roll your own encryption unless you really know what you're doing (in which case you wouldn't be asking questions here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文