Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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);
           }
       }
Posted
Comments
Jibesh 14-Jan-13 15:49pm    
The code you shared doesnt contains enough information to answer. on executing which line you are getting exceptions.
can you copy paste the Exception Stack. you can use the Improve question link right bottom of your question to modify your question.

1 solution

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
C#
var pkCert = _CertFile.PublicKey.Key;
using(var rsaCryptoServiceProvider = (RSACryptoServiceProvider) pkCert){

}


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.
 
Share this answer
 
Comments
[no name] 15-Jan-13 1:00am    
That's exactly what was happening, the using block kept disposing my resouces

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900