How to work with CryptoAPI via C#?

VoimiX picture VoimiX · Jul 7, 2011 · Viewed 11.7k times · Source

There is a group of CryptoApi functions which works with crypto service providers (CSP).

CPAcquireContext
CPCreateHash
CPDecrypt
CPDeriveKey
CPDestroyHash
CPDestroyKey
CPDuplicateHash
CPDuplicateKey
CPEncrypt
CPExportKey
CPGenKey
CPGenRandom
CPGetHashParam
CPGetKeyParam
CPGetProvParam
CPGetUserKey
CPHashData
CPHashSessionKey
CPImportKey
CPReleaseContext
CPSetHashParam
CPSetKeyParam
CPSetProvParam
CPSignHash
CPVerifySignature

Yes, I know that there is System.Cryptography namespace. But I don't need their implementations.

Is there any ready library which provides a .NET wrapper to these functions?

Answer

sehe picture sehe · Jul 7, 2011

There has been an extended article on MSDN about this topic:

Extending .NET Cryptography with CAPICOM and P/Invoke

An article on using CryptoAPI Certificate Stores from .Net.

Some P/Invoke declarations lifted from this blog post that shows the p/Invoke definitions for it:

[snip] code sample too large for SO [/snip]