none
C# x509 usb certificate token hangs application RRS feed

  • Question

  • 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.

    Saturday, November 24, 2018 10:22 AM

All replies

  • Hi DLaurentiu,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about the C# development. Since our forum is to discuss the C++ development, I will help you move this thread to the appropriate forum for a better support.

    Thank you for your understanding.

    Best regards,

    Jack


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 26, 2018 1:27 AM