📄 contactlessdemovcdlg.cpp
字号:
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CContactlessDemoVCDlg::OnQueryDragIcon()
{
return(HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnWritemifkey()
{
UCHAR ucMifareKey[8] = {0};
ULONG ulMifareKeyLen = 0;
char Reader[80]=" ";
UCHAR ucKeyNr[1]={0};
ULONG ulKeyNrLen = 0;
ULONG ulTrKeyNr = 0;
BOOLEAN fSecuredTransmission = FALSE;
UpdateData(TRUE);
if (m_intMifTrOption == 1)
{
fSecuredTransmission = FALSE;
if (m_strMifKeyToReader.GetLength() != 12)
{
m_ctrOutput.AddString("The Key must be of 6 bytes");
TurnOnErrorLed();
goto EXIT;
}
}
else
if (m_intMifTrOption == 0)
{
fSecuredTransmission = TRUE;
if (m_strMifKeyToReader.GetLength() != 16)
{
m_ctrOutput.AddString("The Key must be of 8 bytes");
TurnOnErrorLed();
goto EXIT;
}
CStringToUchar(m_strMifTrKeyNr,ucKeyNr,&ulKeyNrLen);
ulTrKeyNr = ucKeyNr[0];
}
else
{
m_ctrOutput.AddString("One transmission option must be selected");
TurnOnErrorLed();
goto EXIT;
}
CStringToUchar(m_strMifKeyToReader,ucMifareKey,&ulMifareKeyLen);
CStringToUchar(m_strKeyNrToReader,ucKeyNr,&ulKeyNrLen);
ulMifKeyToReaderNr = ucKeyNr[0];
if (fCM5121Selected == TRUE)
{
strcpy(Reader,(LPCTSTR) m_strReaderName);
if (fCardConnected != TRUE)hCard = 0x00000000;
GetSystemTime(&startTime);
lErr = SCardCLWriteMifareKeyToReader(hCard,hContext,Reader,ulMifKeyToReaderNr,ulMifareKeyLen,
ucMifareKey,fSecuredTransmission,ulTrKeyNr);
GetSystemTime(&stopTime);
if (lErr == NO_ERROR)
{
m_ctrOutput.AddString("The key has been written succesfully");
ShowTimeRequired();
}
else
{
ShowErrorMessage(lErr);
}
}
else
{
m_ctrOutput.AddString("For any operation one reader must be selected");
TurnOnErrorLed();
}
EXIT:
ShowCursorOutScreen();
m_strMifKeyToReader.Empty();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnMifauthent()
{
UCHAR ucMifareAuthMode;
UCHAR ucMifareKey[6] = {0};
ULONG ulMifareKeyLen = 0;
UCHAR ucMifareAccessType;
UCHAR ucMifareKeyNr;
UpdateData(TRUE);
if (m_intAuthentMode == 0)ucMifareAuthMode = MIFARE_AUTHENT1A;
else
if (m_intAuthentMode == 1)ucMifareAuthMode = MIFARE_AUTHENT1B;
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("One Authentication mode must be selected");
goto EXIT;
}
if (m_intAccessOption == 1)
{
ucMifareAccessType = MIFARE_KEY_INPUT;
if (m_strAuthentKey.GetLength() != 12)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Key length must be 6");
goto EXIT;
}
CStringToUchar(m_strAuthentKey,ucMifareKey,&ulMifareKeyLen);
}
else
if (m_intAccessOption == 0)
{
CStringToUchar(m_strMifAuthentKeyNr,ucMifareKey,&ulMifareKeyLen);
ucMifareKeyNr = ucMifareKey[0];
ucMifareAccessType = MIFARE_KEYNR_INPUT;
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("One Key option must be selected");
goto EXIT;
}
if (fCardConnected == TRUE)
{
if (m_strCardName == "MIFARE_ST1K" && m_longBlockNr >63)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Block number is not valid");
goto EXIT;
}
if (m_strCardName == "MIFARE_ST4K" && m_longBlockNr >255)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Block number is not valid");
goto EXIT;
}
GetSystemTime(&startTime);
lErr = SCardCLMifareStdAuthent(hCard,m_longBlockNr,ucMifareAuthMode,ucMifareAccessType,ucMifareKeyNr,
ucMifareKey, ulMifareKeyLen);
GetSystemTime(&stopTime);
if (lErr==NO_ERROR)
{
m_ctrOutput.AddString("Authentication has been done successfully");
ulAuthenticatedBlockNr= m_longBlockNr;
ShowTimeRequired();
}
else
{
m_ctrOutput.AddString("Error in Authentication, may be the key is not correct");
ShowErrorMessage(lErr);
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Card is not connected");
}
EXIT:
ShowCursorOutScreen();
m_strAuthentKey.Empty();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnRead()
{
UCHAR ucMifareDataRead[16] ={0};
ULONG ulMifareNumOfDataRead = 0;
char str[80] = "";
m_strDataRead.Empty();
UpdateData(TRUE);
if (fCardConnected == TRUE)
{
if (m_strCardName == "MIFARE_ST1K" || m_strCardName == "MIFARE_ST4K" )
{
if (m_longBlockNr<128)
{
if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
else
{
if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
}
GetSystemTime(&startTime);
lErr = SCardCLMifareStdRead(hCard,m_longBlockNr,ucMifareDataRead,16,&ulMifareNumOfDataRead);
GetSystemTime(&stopTime);
if (lErr == NO_ERROR)
{
UcharToStr(ucMifareDataRead,ulMifareNumOfDataRead,str);
m_strDataRead = str;
m_ctrOutput.AddString("Reading has been done successfully");
ShowTimeRequired();
}
else
{
ShowErrorMessage(lErr);
}
}
else
{
TurnOnErrorLed();
m_ctrOutput.AddString("The Card is not connected");
}
EXIT:
ShowCursorOutScreen();
UpdateData(FALSE);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CContactlessDemoVCDlg::OnWrite()
{
ULONG ulBufferLen = 0;
UCHAR ucDataToWrite[16]={0};
SectorTrailerDlg hSectorTrailerDlg;
m_strDataToWrite.Empty();
UpdateData(TRUE);
if (fCardConnected == TRUE)
{
if (m_strCardName == "MIFARE_ST1K" || m_strCardName == "MIFARE_ST4K" )
{
if (m_longBlockNr == 0)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 0 can not be written");
goto EXIT;
}
if (m_longBlockNr<128)
{
if (ulAuthenticatedBlockNr/4 != ((ULONG)m_longBlockNr)/4)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
else
{
if (ulAuthenticatedBlockNr /16 != ((ULONG)m_longBlockNr)/16)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The selected block sector is not authenticated");
goto EXIT;
}
}
if (m_longBlockNr < 128)
{
if (((m_longBlockNr + 1)%4)==0)
{
if (hSectorTrailerDlg.DoModal()!= IDOK)
goto EXIT;
}
}
else
if (m_longBlockNr> 128)
{
if (((m_longBlockNr + 1)%16)==0)
{
if (hSectorTrailerDlg.DoModal()!= IDOK)
goto EXIT;
}
}
}
if (m_strCardName == "MIFARE_ULIT")
{
if (m_longBlockNr == 0 || m_longBlockNr == 1)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 0 and 1 can not be written");
goto EXIT;
}
if (m_longBlockNr == 2)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 2 internal and lock block, modifying lock block will lock the pages as Read Only, block 2 is not allowed to write by this application ");
goto EXIT;
}
if (m_longBlockNr == 3)
{
TurnOnErrorLed();
m_ctrOutput.AddString("Block 3 is OTP, block 3 is not allowed to write by this application");
goto EXIT;
}
}
CStringToUchar(m_strDataToWrite,ucDataToWrite,&ulBufferLen);
if (ulBufferLen != 16)
{
TurnOnErrorLed();
m_ctrOutput.AddString("The length of the data to be written must be 16");
goto EXIT;
}
GetSystemTime(&startTime);
lErr = SCardCLMifareStdWrite(hCard, m_longBlockNr,ucDataToWrite,ulBufferLen);
GetSystemTime(&stopTime);
if (lErr != NO_ERROR)
{
ShowErrorMessage(lErr);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -