C# X509 usb certificate hangs application





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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.










share|improve this question























  • I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

    – jdweng
    Nov 23 '18 at 22:04











  • There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

    – D.Laurentiu
    Nov 24 '18 at 6:24











  • Did you check the event viewer?

    – jdweng
    Nov 24 '18 at 8:10











  • Yes. Unfortunately, there are no entries

    – D.Laurentiu
    Nov 24 '18 at 8:33











  • Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

    – jdweng
    Nov 24 '18 at 8:58




















0















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.










share|improve this question























  • I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

    – jdweng
    Nov 23 '18 at 22:04











  • There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

    – D.Laurentiu
    Nov 24 '18 at 6:24











  • Did you check the event viewer?

    – jdweng
    Nov 24 '18 at 8:10











  • Yes. Unfortunately, there are no entries

    – D.Laurentiu
    Nov 24 '18 at 8:33











  • Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

    – jdweng
    Nov 24 '18 at 8:58
















0












0








0








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.










share|improve this 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.







c# smartcard x509






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 21:07









D.LaurentiuD.Laurentiu

62




62













  • I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

    – jdweng
    Nov 23 '18 at 22:04











  • There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

    – D.Laurentiu
    Nov 24 '18 at 6:24











  • Did you check the event viewer?

    – jdweng
    Nov 24 '18 at 8:10











  • Yes. Unfortunately, there are no entries

    – D.Laurentiu
    Nov 24 '18 at 8:33











  • Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

    – jdweng
    Nov 24 '18 at 8:58





















  • I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

    – jdweng
    Nov 23 '18 at 22:04











  • There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

    – D.Laurentiu
    Nov 24 '18 at 6:24











  • Did you check the event viewer?

    – jdweng
    Nov 24 '18 at 8:10











  • Yes. Unfortunately, there are no entries

    – D.Laurentiu
    Nov 24 '18 at 8:33











  • Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

    – jdweng
    Nov 24 '18 at 8:58



















I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

– jdweng
Nov 23 '18 at 22:04





I don't think the issue is with the X509 usb certificate. I may be wrong. I suspect it is with the closing of the form. I bet if you comment out the code posted you will still get the hanging. Is the project a Window Form Project? Do you have only one class in the project? Is the form the main form? I suspect you are getting an unhandled exception during the exit. It sound like it is in the Net Library. I would open in control panel the event viewer and check for exceptions at the time the app was run.

– jdweng
Nov 23 '18 at 22:04













There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

– D.Laurentiu
Nov 24 '18 at 6:24





There is no other code inside the application. Commenting the above x509 code causes normal behavior on exit. There is no exception upon exit.

– D.Laurentiu
Nov 24 '18 at 6:24













Did you check the event viewer?

– jdweng
Nov 24 '18 at 8:10





Did you check the event viewer?

– jdweng
Nov 24 '18 at 8:10













Yes. Unfortunately, there are no entries

– D.Laurentiu
Nov 24 '18 at 8:33





Yes. Unfortunately, there are no entries

– D.Laurentiu
Nov 24 '18 at 8:33













Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

– jdweng
Nov 24 '18 at 8:58







Did you check the processes in Task Manager? I would also try a sniffer like wireshark or fiddler. I suspect it is an issue with a virus checker.

– jdweng
Nov 24 '18 at 8:58














0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452955%2fc-sharp-x509-usb-certificate-hangs-application%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452955%2fc-sharp-x509-usb-certificate-hangs-application%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()