Click here to Skip to main content
11,496,146 members (391 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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 5: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

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 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100