Information Security Stack Exchange is a question and answer site for information security professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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#?

share|improve this question
up vote 5 down vote accepted

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. and NCryptoki (the former seems to be open source, not the latter).

share|improve this answer
+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? – LamonteCristo Jun 4 '12 at 11:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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