Search code examples
.netvisual-c++c++-clismartcardsign

Ways of generating a digital signature with .NET Framework


What's the other way of creating a digital signature using a private key (that's on a smart card, with certificate installed in local certificate store) in .NET Framework other than this one, for I have no clue as to how to find out the key container name (and it seems like no one else does)?

CspParameters^ cspa = gcnew CspParameters(ProviderType, ProviderName, keyContainerName, cryptoSecurityKey, securityString);

RSACryptoServiceProvider^ csp = gcnew RSACryptoServiceProvider(cspa);

csp.SignHash()

The way I understand it, if one initializes CspParameters with no keyContainerName, Crypto Provider has no way of telling what certificate one wants to use for signing, so it just won't work. I've searched the net for info and none found (except for a couple unanswered questions). The only examples I have found use strings like "example", "test" for keyContainerName. In case of a program which is going to be used by lots of people with lots of certificates and smart cards, this is useless because you need a method that looks at a chosen certificate and determines its container name (like certutils.exe does) and then creates CspParameters, RSACryptoServiceProvider and then signs. So this is out of question now.

So what are other ways of generating a digital signature with a smart card in .NET Framework?


Solution

  • In case someone else is looking for an answer to this question, this article shows the way:

    In a nutshell, in C++/CLI it can be done this way:

    X509Certificate2^ certificate = //... get your certificate that has a corresponding private key
    
    RSACryptoServiceProvider^ csp = safe_cast<RSACryptoServiceProvider^>(certificate->PrivateKey);
    
    csp.SignHash();
    

    However, it is still unclear as to how to pass a password to RSACryptoServiceProvider if one doesn't want to invoke the native CSP PIN input dialog box.