📄 pkcsdemodlg.cpp
字号:
m_strInfo = "";
UpdateData(FALSE);
}
void CPKCSDemoDlg::OnBtnConnect()
{
if(m_hSession)
return;
::SetCursor(::LoadCursor(NULL, IDC_WAIT));
StartOP();
CK_RV rv;
CK_ULONG ulCount = 0;
rv = C_GetSlotList(TRUE, NULL_PTR, &ulCount);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't acquire information of slot, ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
if(0 >= ulCount)
{
ShowMsg(NEWLINE"Can't connect to token, make sure one token has been inserted."NEWLINE);
return;
}
m_pSlotList = (CK_SLOT_ID_PTR)new CK_SLOT_ID[ulCount];
if (! m_pSlotList)
{
ShowMsg(NEWLINE"Not enough memory!"NEWLINE);
return;
}
rv = C_GetSlotList(TRUE, m_pSlotList, &ulCount);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't acquire information of slot, ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
if(0 >= ulCount)
{
ShowMsg(NEWLINE"Can't connect to token, make sure one token has been inserted."NEWLINE);
return;
}
rv = C_OpenSession(
m_pSlotList[0], CKF_RW_SESSION | CKF_SERIAL_SESSION,
&m_pApplication, NULL_PTR, &m_hSession);
if(CKR_OK != rv )
{
ShowErr(NEWLINE"Can't Acquire information of slot, ErrorCode: 0x%08X."NEWLINE, rv);
delete[] m_pSlotList;
m_pSlotList = NULL_PTR;
}
else
{
ShowMsg(NEWLINE"Connect to token Successfully !"NEWLINE);
m_btnConnect.EnableWindow(FALSE);
ShowMsg(NEWLINE"Before next test step, need to generate RSA key pair!"NEWLINE);
m_btnKeyPairGen.EnableWindow(TRUE);
}
}
void CPKCSDemoDlg::OnBtnKeypairgen()
{
StartOP();
CK_TOKEN_INFO tokenInfo = {0};
CK_RV rv = C_GetTokenInfo(m_pSlotList[0], &tokenInfo);
if (CKR_OK != rv)
{
ShowMsg(NEWLINE"Can not get token information!"NEWLINE);
return;
}
DlgUserPIN* dlgUserPIN = new DlgUserPIN;
dlgUserPIN->DoModal();
delete dlgUserPIN;
if("" == g_strUserPIN)
{
ShowMsg(NEWLINE"You should enter User PIN before generate RSA Key pair!"NEWLINE);
return;
}
CK_ULONG ulPIN = g_strUserPIN.GetLength();
CK_BYTE_PTR pPIN = (CK_BYTE_PTR)g_strUserPIN.GetBuffer(ulPIN);
::SetCursor(::LoadCursor(NULL, IDC_WAIT));
rv = C_Login(m_hSession, CKU_USER, pPIN, ulPIN);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Can't use your User PIN login to token ,ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
else
ShowMsg(NEWLINE"Logging in to token Successfully!"NEWLINE);
::SetCursor(::LoadCursor(NULL, IDC_WAIT));
rv = C_GenerateKeyPair(
m_hSession, &keyGenMechanism,
pubTemplate, countof(pubTemplate),
priTemplate, countof(priTemplate),
&m_hPubKey, &m_hPriKey);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Can't generate RSA key pair, ErrorCode: 0x%08X."NEWLINE, rv);
return;
}
else
{
m_btnKeyPairGen.EnableWindow(FALSE);
m_btnSign.EnableWindow(TRUE);
m_btnEncrypt.EnableWindow(TRUE);
ShowMsg(NEWLINE"Genrate Key Pair Successfully!"NEWLINE);
ShowMsg(NEWLINE"Now,You can Sign,Verify,Encryp and Decrypt by using the Key Pair!"NEWLINE);
m_bKeyGen = TRUE;
}
}
void CPKCSDemoDlg::OnCancel()
{
OnOK();
}
void CPKCSDemoDlg::OnOK()
{
CK_RV rv;
if(m_bKeyGen)
{
StartOP();
BOOL bRet = TRUE;
ShowMsg(NEWLINE"Clear Key Pair which was generated!"NEWLINE);
UpdateWindow();
::SetCursor(::LoadCursor(NULL, IDC_WAIT));
rv = C_DestroyObject(m_hSession, m_hPubKey);
if(CKR_OK != rv)
{
bRet = FALSE;
ShowErr(NEWLINE"Can't delete public key ,Error code 0x%08X."NEWLINE, rv);
}
rv = C_DestroyObject(m_hSession, m_hPriKey);
if(CKR_OK != rv)
{
bRet = FALSE;
ShowErr(NEWLINE"Can't delete private key ,Error code 0x%08X."NEWLINE, rv);
}
if(bRet)
ShowMsg(NEWLINE"Clear Key Pair that was generated successfully!"NEWLINE);
}
CDialog::OnOK();
}
void CPKCSDemoDlg::OnBtnSign()
{
StartOP();
CK_RV rv;
rv = C_SignInit(m_hSession, &ckMechanism, m_hPriKey);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to call SignInit!Error code 0x%08X."NEWLINE, rv);
return;
}
rv = C_Sign(m_hSession,
pbMsg,
ulMsgLen,
m_pSignature, &m_ulSignatureLen);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to Sign!Error code 0x%08X."NEWLINE, rv);
return;
}
else
{
ShowMsg(NEWLINE"Data:"NEWLINE);
ShowMsg((char*)pbMsg);
ShowMsg(NEWLINE" was Signed successfully!"NEWLINE"Signed Data is:"NEWLINE);
ShowMsg(nByteToStr(m_ulSignatureLen, m_pSignature, 1, 16));
ShowMsg(NEWLINE""NEWLINE"Now you can do RSA Verification!"NEWLINE);
m_btnVerify.EnableWindow(TRUE);
}
}
void CPKCSDemoDlg::OnBtnVerify()
{
StartOP();
CK_RV rv;
rv = C_VerifyInit(m_hSession, &ckMechanism, m_hPubKey);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Failed to call VerifyInit!Error code 0x%08X."NEWLINE, rv);
return;
}
rv = C_Verify(m_hSession,
pbMsg, ulMsgLen,
m_pSignature, m_ulSignatureLen);
if(CKR_OK != rv)
ShowErr(NEWLINE"Fail to call verify!Error code 0x%08X."NEWLINE, rv);
else
ShowMsg(NEWLINE"Verify Successfully!"NEWLINE);
}
void CPKCSDemoDlg::OnBtnClearinfo()
{
m_strInfo = "";
UpdateData(FALSE);
}
void CPKCSDemoDlg::OnBtnEncrypt()
{
StartOP();
CK_RV rv;
if(m_pbCipherBuffer)
{
delete[] m_pbCipherBuffer;
m_pbCipherBuffer = NULL_PTR;
m_ulCipherLen = 0;
}
rv = C_EncryptInit(m_hSession,
&ckMechanism,
m_hPubKey);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to call EncryptInit !Error code 0x%08X."NEWLINE, rv);
return;
}
rv = C_Encrypt(m_hSession, pbMsg, ulMsgLen, NULL_PTR, &m_ulCipherLen);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Can't acquire the size of Data After encrypt,Error code 0x%08X."NEWLINE, rv);
return;
}
m_pbCipherBuffer = (CK_BYTE_PTR)new CK_BYTE[m_ulCipherLen];
if (! m_pbCipherBuffer)
{
ShowMsg(NEWLINE"Can't allocate enough memory!"NEWLINE);
return;
}
ZeroMemory(m_pbCipherBuffer, m_ulCipherLen);
rv = C_Encrypt(m_hSession, pbMsg, ulMsgLen, m_pbCipherBuffer, &m_ulCipherLen);
if (CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to encrypt!Error code 0x%08X."NEWLINE, rv);
return;
}
ShowMsg(NEWLINE"Data :"NEWLINE);
ShowMsg((char*)pbMsg);
ShowMsg(NEWLINE"was encrypted successfully!"NEWLINE"Encryted Data is:"NEWLINE);
ShowMsg(nByteToStr(m_ulCipherLen, m_pbCipherBuffer, 1, 16));
ShowMsg(NEWLINE""NEWLINE"Now You can decrypt by using RSA private key!"NEWLINE);
m_btnDecrypt.EnableWindow(TRUE);
}
void CPKCSDemoDlg::OnBtnDecrypt()
{
StartOP();
CK_BYTE_PTR pbRestoredMsg = NULL;
CK_ULONG ulRestoredMsgLen = 0;
CK_RV rv;
rv = C_DecryptInit(m_hSession,
&ckMechanism,
m_hPriKey);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to call DecryptInit!Error code 0x%08X."NEWLINE, rv);
return;
}
rv = C_Decrypt(m_hSession, m_pbCipherBuffer, m_ulCipherLen, NULL_PTR, &ulRestoredMsgLen);
if(CKR_OK != rv)
{
ShowErr(NEWLINE"Can't acuire size of Data after Decrypt! Error code 0x%08X."NEWLINE, rv);
return;
}
pbRestoredMsg = (CK_BYTE_PTR)new CK_BYTE[ulRestoredMsgLen + 1];
if (! pbRestoredMsg)
{
ShowMsg(NEWLINE"Can't allocate enough memory for Decrypt!"NEWLINE);
return;
}
ZeroMemory(pbRestoredMsg, ulRestoredMsgLen + 1);
rv = C_Decrypt(m_hSession, m_pbCipherBuffer, m_ulCipherLen, pbRestoredMsg, &ulRestoredMsgLen);
if (CKR_OK != rv)
{
ShowErr(NEWLINE"Fail to call decrypt,Error code 0x%08X."NEWLINE, rv);
delete[] pbRestoredMsg;
return;
}
ShowMsg(NEWLINE"Decrypt Successfulluy, Data after Decrypt is :"NEWLINE);
ShowMsg(pbRestoredMsg);
ShowMsg(NEWLINE);
delete[] pbRestoredMsg;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -