C# x509 usb certificate token hangs application

  • Thread starter Thread starter DLaurentiu
  • Start date Start date
D

DLaurentiu

Guest
I'm trying to find out why my application hangs on exit, after the use of a usb token certificate. Without any try/catch blocks or other logic, the code is:

X509Store s= new X509Store(StoreName.My, StoreLocation.CurrentUser);
s.Open(OpenFlags.OpenExistingOnly);
X509Certificate2 cert = s.Certificates[0];
s.Close();

RSACryptoServiceProvider rsa = cert.PublicKey.Key as RSACryptoServiceProvider;
byte[] cryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes("test"), true);

rsa = cert.PrivateKey as RSACryptoServiceProvider;
string x = Encoding.UTF8.GetString(rsa.Decrypt(cryptedData, true));
When I run the code inside a button click event, I am prompted for the PIN of the USB token. After I type and confirm the PIN , the variable x == "test", which is to be expected. Therefore encryption and decryption with the token work.

When I close the form, the application just hangs, and, if I click "break all" in VS2017, even visual studio hangs. My guess is that something related to the usb-token cryptography functions is not released/closed.

I've tried, without any luck, with:

Environment.Exit(0);
cert.Reset();
rsa.Clear();
rsa.Dispose();
cert.Dispose();
Any ideas? Thank you.



Continue reading...
 
Back
Top