关于usbkey验pin的代码
我现在的做法,
第一
C_OpenSession(
m_pSlotList[0],
m_SessionFlags, //CKF_RW_SESSION | CKF_SERIAL_SESSION,
(CK_VOID_PTR)szApplication,
NULL_PTR,
&m_hSession);
通过m_hSession判断是否插入钥匙;
第二
HCRYPTPROV hCryptProv = NULL;
HCRYPTKEY hExPubKey = NULL;
BYTE a[128] = "CYBER";
DWORD lenth;
lenth = strlen((char *)a);
//获取密钥容器
if (!CryptAcquireContext(
&hCryptProv,
NULL,
TEST_CSP_NAME,
PROV_RSA_FULL,
NULL))
{
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
if(hExPubKey)
{
CryptDestroyKey(hExPubKey);
}
return -1;
}
//获取密钥句柄
if (!CryptGetUserKey(
hCryptProv,
AT_KEYEXCHANGE,
&hExPubKey))
{
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
if (hExPubKey)
{
CryptDestroyKey(hExPubKey);
}
return -2;
}
if (!CryptEncrypt(hExPubKey, 0, TRUE, 0, a, &lenth, 128))
{
cyber_error_f("auVerifyPinByCSP@LibPkcs11 加密失败");
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
if (hExPubKey)
{
CryptDestroyKey(hExPubKey);
}
return -3;
}
if (!CryptDecrypt(hExPubKey, 0, TRUE, 0, a, &lenth))
{
cyber_error_f("auVerifyPinByCSP@LibPkcs11 错误PIN码");
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
if (hExPubKey)
{
CryptDestroyKey(hExPubKey);
}
return -4;
}
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
if (hExPubKey)
{
CryptDestroyKey(hExPubKey);
}
return 1;
}
如果成功放回1 验pin通过
现在的问题是插入钥匙第一次验通过以后,钥匙不拔,在调用上面的过程验pin就直接通过了 不会弹出输入pin码的窗口,是什么问题呢? 是不是CryptDecrypt(hExPubKey, 0, TRUE, 0, a, &lenth)中的TRUE要改成FALSE??