我们以前曾经使用过这种方法。
byte[] pinBuffer = Encoding.ASCII.GetBytes(pinCode);
IntPtr providerHandle = IntPtr.Zero;
SafeNativeMethods.Execute(() => SafeNativeMethods.CryptAcquireContext(
ref providerHandle,
"keyContainer",
"My HSM Removable Token Cryptographic Provider",
1,
64));
SafeNativeMethods.Execute(() => SafeNativeMethods.CryptSetProvParam(
providerHandle,
32,
pinBuffer,
0U));
SafeNativeMethods.Execute(() => SafeNativeMethods.CertSetCertificateContextProperty(
cert.Handle,
1,
0U,
providerHandle));
它不再起作用了。
我在CryptAcquireContext上发生内部错误。
还有另一种无需打开弹出窗口即可检索证书的方法吗?
相同错误
IntPtr providerHandle = IntPtr.Zero;
var cspParameters = new CspParameters()
{
ParentWindowHandle = providerHandle,
KeyContainerName = "keyContainer",
ProviderName = "My HSM Removable Token Cryptographic Provider",
ProviderType = 1,
Flags = CspProviderFlags.NoPrompt,
};
var rsaCryptoServiceProvider = new RSACryptoServiceProvider(cspParameters);
似乎是NoPrompt(64)标志
答案 0 :(得分:0)
如果您只需要从智能卡获取证书,则可以使用以下flag:
// dwFlags definitions for CryptAcquireContext
#define CRYPT_VERIFYCONTEXT 0xF0000000
CSP不会尝试获取私钥,因此没有PIN对话框。
从WinCrypt.h
复制粘贴:
#define CERT_KEY_PROV_HANDLE_PROP_ID 1
#define CRYPT_SILENT 0x00000040