Is anyone aware of a C# code sample to interact with smartcard, hardware security module, TPM chips (etc), that will allow me to encrypt and decrypt data?

My goal is to allow my application to support a variety of key stores, and should the administrator decide they want the Private key to reside on those devices, I want to be able to support that.

Is there a consistent, or universal way to access these devices? Is there a way through C#?


In the Windows world, cryptography with special hardware goes through the "Cryptographic Service Providers" (CSP). A CSP is a module registered in the OS, which offers access to such a kind of hardware.

From C#, you can look at RSACryptoServiceProvider, which can be created with some parameters which designate a specific CSP.

The non-Windows world tends to use PKCS#11 for that. Google finds some software packages which can apparently use a PKCS#11 DLL (i.e. the driver for the crypto hardware) from C#; e.g. pkcs11.net and NCryptoki (the former seems to be open source, not the latter).

  • +1. In other words - you need the "driver" for the specific hardware. One tip - make sure to look at the CSPParameters class, too...
    – AviD
    Jan 20 '11 at 0:52
  • Using C# on non-Windows is not a nice idea anyway. If your application is in fact a user-facing application (meaning you're more interested in accessing a smart card rather than a HSM) you're better off with CryptoAPI than PKCS#11, as CryproAPI layer takes better care of usability issues than PKCS#11 (think: certificate selection, PIN entry etc) While it is generally possible to have "universal" crypto interfaces, I suggest to consider user experience and usability as well. Choose usability and implementation complexity over portability and ease of coding.
    – martin
    Jun 14 '11 at 8:12
  • Related: Should I use Microsoft's RSA CSP or AES CSP for best interoperability? security.stackexchange.com/q/15626/396 Jun 4 '12 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.