Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
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)
               // 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,
                       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.
                       // Why convert to base 64?
                       // Because it is the largest power-of-two base printable using only
                       // ASCII characters
               return stringBuilder.ToString();
           catch (Exception ex)
               throw new Exception(ex.Message);
Posted 14-Jan-13 4:50am
jibesh at 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Mathlab at 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)

  Print Answers RSS
0 CHill60 360
1 OriginalGriff 203
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Peter Leow 145

Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid