How would I integrate Hardware Security Module encryption with a C# application?
How do I integrate HSM encryption with C#?
11.5k views Asked by user690932 AtThere are 3 answers
HSM typically means Hardware Security Module. This is a device that will usually physically protect private or secret keys such that they don't ever get into your computer's RAM. Most HSMs will do encryption and signatures for you rather than just holding keys.
Access to a HSM's crypto powers can be via a handful of APIs. Including PKCS#11, Chil (OpenSSL). MSCAPI and CNG provders also exist to use HSMs.
Most HSM vendors will provide you with a PKCS#11 library or CAPI/CNG provider. Once you have this, it is a matter of programming against a published API.
Generally, using a HSM goes somthing along these lines:
provider = HSM.Connect()
keyhandle = provider.LoadKey("my_rsa_key")
signature = provider.Sign( keyhandle, "Sha1WithRSA", "myData" )
provider.UnloadKey( keyhandle )
Unfortunately, It the managed portion of CAPI and CNG do not allow for access to third-party providers which you would need to use a CAPI/CNG HSM via C#. You will have to call directly into the unmanaged CAPI/CNG or a PKCS#11 library code using PInvoke calls.
We used Pkcs11Interop and it worked really well. It is an Apache 2.0 licensed open source library. As far as i see it is quite up to date and still being maintained.
If its a PKCS #11 compliant device, you can use NCryptoki. From their website: