Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

CryptAcquireContext exception

$
0
0

internal class Crypt
{
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool CryptAcquireContext(
ref IntPtr hProv,
string pszContainer,
string pszProvider,
uint dwProvType,
uint dwFlags);

}

if (!Crypt.CryptAcquireContext(ref _providerHandle,
KeyStore,
provider,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET | CRYPT_SILENT))
{
int rc = Marshal.GetLastWin32Error();
if ((uint)rc == NTE_BAD_KEYSET)
{
if (!Crypt.CryptAcquireContext(ref _providerHandle,
KeyStore,
provider,
PROV_RSA_FULL,
CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET | CRYPT_SILENT))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "CryptAcquireContext Create");
}
}
else
{
throw new Win32Exception(rc, "CryptAcquireContext");
}
}

CryptAcquireContext is always throwing an exception in the above code any ideas why ?


Viewing all articles
Browse latest Browse all 4737

Trending Articles