RSACryptoServiceProvider KeyContainer 似乎超时?

发布于 2024-09-16 18:39:44 字数 3046 浏览 8 评论 0原文

我正在使用这样的 RSACryptoServiceProvider...

 private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;

            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
            {
                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  

                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException ex)
        {
            sl.Write(ex, MessageType.Error);
            throw;                
        }
    }

然后,我在关闭 Outlook 插件 Windows 窗体应用程序并将其重新打开(这就是使用这段代码的内容)后尝试解密数据。解密代码看起来像这样......

private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;

            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
            {
                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  

                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException ex)
        {
            sl.Write(ex, MessageType.Error);
            throw;                
        }
    }    

工作得很好,直到出现一些我无法确定的情况。不知道是不是日期变了还是什么。发生的情况是,我尝试解密数据,但收到“错误数据”错误。现在,它再次工作得很好,直到经过一段时间,或者关闭应用程序,或者用户注销。我只是不知道也无法确定是什么原因造成的。当我清除来自文本文件的加密数据并重新创建它并解密它时,我没有问题。即使我在加密/保存到文件和从文件读取/解密之间重新启动应用程序,它也会很好用!发生了一些事情,我只是不太了解 KeyContainers,无法理解什么可能使 CspParameters 过期是我最好的猜测?

I am using the RSACryptoServiceProvider like this...

 private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;

            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
            {
                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  

                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException ex)
        {
            sl.Write(ex, MessageType.Error);
            throw;                
        }
    }

I then try to decrypt the data after turning off my Outlook Plugin Windows Form application and turning it back on which is what is using this peice of code. The decrypt code looks like this...

private byte[] RSAEncrypt(byte[] DataToEncrypt, string ContainerName, bool DoOAEPPadding)
    {
        try
        {
            byte[] encryptedData;

            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an RSA container.  The default is 1.
            CspParameters cspParams = new CspParameters();

            // Specify the container name using the passed variable.
            cspParams.KeyContainerName = ContainerName;

            cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

            //Create a new instance of RSACryptoServiceProvider.
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams))
            {
                //Encrypt the passed byte array and specify OAEP padding.  
                //OAEP padding is only available on Microsoft Windows XP or
                //later.  

                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
            }
            return encryptedData;
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch (CryptographicException ex)
        {
            sl.Write(ex, MessageType.Error);
            throw;                
        }
    }    

Works great until something comes up that I can not put my finger on. I don't know if it is like the date changes or what. What happens is that I try to decrypt the data and I get a "bad data" error. Now again it works great until some elapsed period of time, or turning off the app, or the user logging off. I just don't know and can't determine what causes it. The moment I blow away the encrypted data which comes from a text file and recreate it and decrypt it I have no problem. Even if I restart the application in between encrypting/saving to file and the reading from file/decrypting it will works great! Something happens and I just don't know KeyContainers well enough to understand what could possibly make the CspParameters expire is my best guess?

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

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

发布评论

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

评论(3

九八野马 2024-09-23 18:39:44

如果你真的遇到困难,你可以尝试使用充气城堡加密库:

http://www.bouncycastle.org /csharp/

You could try using the bouncy castle crypto libraries if you're really stuck:

http://www.bouncycastle.org/csharp/

御守 2024-09-23 18:39:44

我最终使用了 CspParameters 标志,而不是使用用户 KeyContainer 存储,而是使用了 Machine KeyContainer 存储。

I ended up using the CspParameters flag and instead of using the Users KeyContainer store I used the Machine KeyContainer Store.

旧时光的容颜 2024-09-23 18:39:44

是的,如果您设置:
cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

然后密钥容器存储在用户的密钥容器存储中,然后以另一个用户的身份登录并使用 RSA 为您提供一个完全不同的 KeyContainer 存储。

使用这个代替:
cspParams.Flags = CspProviderFlags.UseMachineKeyStore = true;

将使用本地计算机的 KeyContainer 存储(该存储对于计算机而言是全局的),并且无论哪个用户登录,都会为您提供相同的 KeyContainer 存储。但是,这仅适用于该 Windows 安装。在不同的 Windows 安装或计算机下运行您的程序将为您提供不同的 KeyContainer 存储。如果您希望在多台计算机上解密相同的数据,则需要将密钥保存到硬盘驱动器上的文件中。将密钥保存到纯文本文件会带来巨大的安全风险,因此请在将密钥保存到文件之前对其进行加密,或者将其放入受密码保护的 .rar 文件或其他文件中。

如果您仍然遇到问题,请尝试设置:
RSA.PersistKeyInCsp = true;

这将确保您的密钥持久保存在 KeyContainer 存储中。如果您使用 CspParameters 构造函数,则将文件保留在 KeyContainer 中应该是默认行为,例如:

CspParameters cspParams = new CspParameters();

用微软自己的话说:
“这种形式的 CspParameters 将 ProviderType 字段初始化为值 24,该值指定 PROV_RSA_AES 提供程序。”
来源:http://msdn.microsoft.com/en-us/library/xw9ywed4 .aspx

所以你在代码中的注释是不正确的,我会误导你。我建议你纠正它们。

我不确定其他 ProviderType 及其关于将密钥保留在 KeyContainer 存储中的默认设置,因此如果您仍然遇到问题,可能需要将 PersistKeyInCsp 设置为 TRUE。

希望这有帮助。

〜亚当·怀特哈特();

Yes, if you set:
cspParams.Flags = CspProviderFlags.UseDefaultKeyContainer;

then the key container is stored in the user's key container store, then logging on as another user will and using RSA with present you with an entirely different KeyContainer store.

Using this instead:
cspParams.Flags = CspProviderFlags.UseMachineKeyStore = true;

Will use the local machine's KeyContainer store, which is global for the machine, and will provide you with the same KeyContainer store, irregardless of which user is logged in. However, this only applies for that windows installation. Running your program under a different windows installation or machine will provide you with a different KeyContainer store. If you wish to decrypt the same data across multiple machines, you will need to persist your key to a file on the hard drive. Persisting a key to a plain text file is a huge security risk, so please encrypt your key before persisting it to a file, or put it in a password protected .rar files or something.

If your still having issues, try setting:
RSA.PersistKeyInCsp = true;

This will ensure that your key is persisted in the KeyContainer store. Persisting the file in the KeyContainer should be the default behavior if you use the CspParameters constructor such as:

CspParameters cspParams = new CspParameters();

In Microsoft's own words:
"This form of CspParameters initializes the ProviderType field to a value of 24, which specifies the PROV_RSA_AES provider."
Source: http://msdn.microsoft.com/en-us/library/xw9ywed4.aspx

So your comments in your code is incorrect and my be misleading you. I would advise you to correct them.

I am unsure about other ProviderTypes and their default settings regarding persisting the key in the KeyContainer store, so setting PersistKeyInCsp to TRUE might be necessary if your still having issues.

Hope this helps.

~Adam WhiteHat();

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