CryptographicException:未知错误“80007005”。在 .Net Compact Framework 中调用 RSACryptoServiceProvider.Decrypt() 时
我正在尝试使用 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);
有人知道解决方法吗?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修复了代码!我想我毕竟不需要指定容器......
Fixed the code! I guess I do not need to specify a container after all...
通过向此方法传递 false,您将选择不导出私钥。如果没有私钥,将很难解密数据。尝试将 true 传递给导出方法。
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.
使用 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).