Click here to Skip to main content
Sign Up to vote bad
good
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);
           }
       }
Posted 14 Jan '13 - 4:50
Mathlab1.8K

Comments
jibesh - 14 Jan '13 - 15:49
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
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.
  Permalink  
Comments
Mathlab - 15 Jan '13 - 1:00
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,386
1 OriginalGriff 6,596
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid