简体   繁体   English

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

[英]CryptographicException: Unknown Error '80007005'. when calling RSACryptoServiceProvider.Decrypt() in .Net Compact Framework

I am trying to use the RSACryptoServiceProvider to encrypt/decrypt. 我正在尝试使用RSACryptoServiceProvider进行加密/解密。 Encrypting works fine, but the Decrypt method throws an exception with the message: 加密可以正常工作,但是Decrypt方法会在消息中引发异常:

Unknown Error '80007005'. 未知错误“ 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! 谢谢!

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);
rsap1 = rsa1.ExportParameters(false);

By passing false to this method, you're choosing to not export the private key. 通过将false传递给此方法,您选择不导出私钥。 Without the private key it will be difficult to decrypt the data. 没有私钥,将很难解密数据。 Try passing true to the export method. 尝试将true传递给export方法。

When using RSA you need to understand the basics of key management. 使用RSA时,您需要了解密钥管理的基础知识。 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加密任何内容的明显问题了? 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). RSA加密用于加密会话密钥,并且有专用的密钥交换协议来处理此问题(即像TLS这样的面向流或像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. 请注意,这些只是MSDN中的简单代码示例,如果不对加密技术(尤其是密钥管理)有深入的了解,则任何人都不应使用它们。

I recommend you look into using a high level class like SslStream for encrypting data exchanges. 我建议您考虑使用像SslStream这样的高级类来加密数据交换。 For a document storage encryption scheme you better use the OS facilities or rely on ProtectedData class. 对于文档存储加密方案,最好使用OS功能或依赖ProtectedData类。 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). 同样,除非您真的知道自己在做什么,否则不要进行自己的加密(在这种情况下,您将不会在这里提出问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用RSACryptoServiceProvider.Decrypt解密 - Decrypting using RSACryptoServiceProvider.Decrypt RSACryptoServiceProvider.Decrypt() 可以返回不正确的数据吗? - Can RSACryptoServiceProvider.Decrypt() return incorrect data? .NET Compact Framework 中的 RSA RSACryptoServiceProvider 使用来自完整 .NET 框架的公钥 - RSA RSACryptoServiceProvider in .NET Compact Framework using public key from full .NET Framework 尝试将公钥加载到 RSACryptoServiceProvider 时出现 CryptographicException Bad Key - CryptographicException Bad Key when trying to load public key into RSACryptoServiceProvider .Net Compact Framework-调用使用[out] SAFEARRAY(float)的ActiveX对象* - .Net Compact Framework - Calling ActiveX Object that uses [out] SAFEARRAY(float) * Win10 .NET 3.5 RSACryptoServiceProvider.Dispose 抛出 CryptographicException(拒绝访问或共享冲突) - Win10 .NET 3.5 RSACryptoServiceProvider.Dispose throws CryptographicException (ACCESS DENIED or SHARING VIOLATION) .NET RSACryptoServiceProvider 使用 4096 私钥加密,如何在 Android 上对其进行解密 - .NET RSACryptoServiceProvider encrypt with 4096 private key, how to decrypt it on Android 带有.NET Compact Framework的XSockets - XSockets with .NET Compact Framework 使用.net紧凑框架进行本地化 - localization with .net compact framework 在Compact Framework中调用新表单时抛出MissingMethodException - MissingMethodException thrown when calling new form in Compact Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM