“安全手柄已关闭” [英] "Safe handle has been closed"

查看:139
本文介绍了“安全手柄已关闭”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的代码运行时,我收到并且错误地说安全句柄已经关闭。



我的函数中有一个代码块,带有使用语句和一些变量我得到了变量已被处理的异常。



当我第一次调用函数时它运行正常但是第二次时间我运行它抛出的功能安全手柄已关闭,我想要使用的变量已被处理。



I am getting and error that the "safe handle has been closed" when my code is running.

I have a code block in my function with a "using" statement and on some of the variables I get the exception that the variable has been disposed.

When I call the function the first time it runs fine but the second time I run the function it throws the "Safe handle has been closed" and the variable that I want to use has been disposed.

public virtual string Encrypt(string iString)
       {
           try
           {
               // TODO: Add Proper Exception Handlers
               StringBuilder stringBuilder;
               var pkCert = _CertFile.PublicKey.Key;
               using(var rsaCryptoServiceProvider = (RSACryptoServiceProvider) pkCert)
               {
                   int keySize = rsaCryptoServiceProvider.KeySize/8;
                   byte[] bytes = Encoding.UTF32.GetBytes(iString);

                   int maxLength = keySize - 42;
                   int dataLength = bytes.Length;
                   int iterations = dataLength/maxLength;
                   stringBuilder = new StringBuilder();
                   for (var i = 0; i <= iterations; i++)
                   {
                       var tempBytes = new byte[
                           (dataLength - maxLength*i > maxLength)
                               ? maxLength
                               : dataLength - maxLength*i];
                       Buffer.BlockCopy(bytes, maxLength*i, tempBytes, 0,
                                        tempBytes.Length);
                       byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, _FOaep);
                       // Be aware the RSACryptoServiceProvider reverses the order of
                       // encrypted bytes. It does this after encryption and before
                       // decryption. If you do not require compatibility with Microsoft
                       // Cryptographic API (CAPI) and/or other vendors. Comment out the
                       // next line and the corresponding one in the DecryptString function.
                       Array.Reverse(encryptedBytes);
                       // Why convert to base 64?
                       // Because it is the largest power-of-two base printable using only
                       // ASCII characters
                       stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
                   }

               }
               return stringBuilder.ToString();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }

推荐答案

是的,我同意评论,共享desen''t包含足够的信息。我试图模拟这个,但我的代码工作正常。



我想,你提到的错误发生在

Yes, I''m agreed with a comment, shared desen''t contains enough information. I tried to simulate this, but my code works fine.

I guess, the error you mentioned is occurred with
var pkCert = _CertFile.PublicKey.Key;
using(var rsaCryptoServiceProvider = (RSACryptoServiceProvider) pkCert){

}





因为using块处理pkCert,所以下次调用时,pkCert为null。 />


您能否通过删除使用块来尝试代码?或者你可以调试并检查_CertFile是否在第二次为空。



because the using block dispose the "pkCert" so that the next time when you calling that, pkCert is null .

Could you please try you code by removing the using block? Or could you please debug and check whether "_CertFile" is null in second time.


这篇关于“安全手柄已关闭”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆