private CryptApi() { const string IntelDefaultProvider = "Intel Hardware Cryptographic Service Provider"; handle = new SafeCryptHandle(); if (NativeMethods.CryptAcquireContext(out handle, null, IntelDefaultProvider, NativeMethods.PROV_INTEL_SEC, 0)) { return; } else if (NativeMethods.CryptAcquireContext(out handle, null, null, NativeMethods.PROV_RSA_FULL, 0)) { return; } else if (Marshal.GetLastWin32Error() == NativeMethods.NTE_BAD_KEYSET) { if (NativeMethods.CryptAcquireContext(out handle, null, null, NativeMethods.PROV_RSA_FULL, NativeMethods.CRYPT_NEWKEYSET)) { return; } } throw new NotSupportedException("Unable to acquire a cryptographic service provider."); }
public static extern bool CryptAcquireContext(out SafeCryptHandle phProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
public static extern bool CryptGenRandom(SafeCryptHandle hProv, uint dwLen, byte[] pbBuffer);
private void Dispose(bool disposing) { if (handle == null) return; if (disposing) handle.Close(); handle = null; }