📄 testrfcarddlg.cpp
字号:
pDateTime->SetFormat("yyy年MM月dd日");
//提取身份证号
memset(pszDisplay,0x00,32);
for(i=0;i<8;i++)
{
sprintf(pszTemp,"%02x",pszCardData[21+i]);
strcat(pszDisplay,pszTemp);
}
pszDisplay[16] = pszCardData[29];
pszDisplay[17] = pszCardData[30];
pszDisplay[18] = pszCardData[31];
GetDlgItem(IDC_EDIT_ID)->SetWindowText(pszDisplay);
//取卡号
memset(pszDisplay,0x00,32);
for(i=0;i<5;i++)
{
sprintf(pszTemp,"%02x",pszCardData[32+i]);
strcat(pszDisplay,pszTemp);
}
pszDisplay[9] = 0x00;
GetDlgItem(IDC_EDIT_CARDID)->SetWindowText(pszDisplay);
//取邮编
memset(pszDisplay,0x00,32);
for(i=0;i<3;i++)
{
sprintf(pszTemp,"%02x",pszCardData[37+i]);
strcat(pszDisplay,pszTemp);
}
GetDlgItem(IDC_EDIT_MAILCODE)->SetWindowText(pszDisplay);
//取电话
memset(pszDisplay,0x00,32);
for(i=0;i<8;i++)
{
sprintf(pszTemp,"%02x",pszCardData[40+i]);
strcat(pszDisplay,pszTemp);
}
for(i=0;i<16;i++)
{
if('f' == pszDisplay[i])
{
pszDisplay[i] = 0x00;
break;
}
}
GetDlgItem(IDC_EDIT_PHONE)->SetWindowText(pszDisplay);
}
int CTestRFCardDlg::StringToHex(char* pszData,unsigned char* pszHex)
{
int nLength;
unsigned char pszTemp[4];
nLength = strlen(pszData)/2;
//转换为HEX格式
for(int i=0;i<nLength;i++)
{
memset(pszTemp,0x00,4);
memcpy(pszTemp,pszData+i*2,2);
pszHex[i] = pszTemp[0]<<4; pszHex[i] += pszTemp[1]&0x0f;
}
return nLength;
}
HANDLE CTestRFCardDlg::SSI_OpenRFReader(int nPort, int nBaudrate)
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char pcCommPort[8];
if(10 > nPort)
sprintf(pcCommPort,"COM%d",nPort);
else
sprintf(pcCommPort,"\\\\.\\COM%d",nPort);
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE) {
// Handle the error.
//printf ("CreateFile failed with error %d.\n", GetLastError());
return (NULL);
}
// We will build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess) {
// Handle the error.
//printf ("GetCommState failed with error %d.\n", GetLastError());
return (NULL);
}
dcb.BaudRate = nBaudrate;
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess) {
// Handle the error.
//printf ("SetCommState failed with error %d.\n", GetLastError());
return (NULL);
}
PurgeComm(hCom,PURGE_TXCLEAR);PurgeComm(hCom,PURGE_RXCLEAR);
DWORD dwTime;
COMMTIMEOUTS commTimeOut;
GetCommTimeouts(hCom,&commTimeOut);
dwTime = commTimeOut.ReadIntervalTimeout;
dwTime = commTimeOut.ReadTotalTimeoutConstant;
dwTime = commTimeOut.ReadTotalTimeoutMultiplier;
dwTime = commTimeOut.WriteTotalTimeoutConstant;
dwTime = commTimeOut.WriteTotalTimeoutMultiplier;
commTimeOut.ReadTotalTimeoutConstant = 100;
commTimeOut.ReadTotalTimeoutMultiplier = 100;
commTimeOut.WriteTotalTimeoutMultiplier = 100;
SetCommTimeouts(hCom,&commTimeOut);
return hCom;
}
void CTestRFCardDlg::SSI_CloseRFReader(HANDLE handlePort)
{
if (handlePort == INVALID_HANDLE_VALUE) {
return ;
}
CloseHandle(handlePort);
}
int CTestRFCardDlg::SSI_GetRFReaderByte(HANDLE handlePort)
{
unsigned char pszBuffer[2];
DWORD dwLength;
if (handlePort == INVALID_HANDLE_VALUE) {
// Handle the error.
//printf ("CreateFile failed with error %d.\n", GetLastError());
return (-1);
}
if(!ReadFile(handlePort,pszBuffer,1,&dwLength,NULL))
return -2;
return pszBuffer[0];
}
int CTestRFCardDlg::SSI_SendRFReaderByte(HANDLE handlePort, unsigned char nByte)
{
unsigned char pszBuffer[64];
DWORD dwLength;
pszBuffer[0] = nByte;
if (handlePort == INVALID_HANDLE_VALUE) {
// Handle the error.
//printf ("CreateFile failed with error %d.\n", GetLastError());
return (-1);
}
if(!WriteFile(handlePort,pszBuffer,1,&dwLength,NULL))
{
return (-2);
}
return 0;
}
int CTestRFCardDlg::SSI_SendStringEx(HANDLE handlePort,unsigned char *pszBuffer,unsigned int nLength)
{
DWORD dwLength;
if((NULL == handlePort) || (handlePort == INVALID_HANDLE_VALUE)) return (-1);
if(WriteFile(handlePort,pszBuffer,nLength,&dwLength,NULL))
return 0;
return (-2);
}
/*
功能:接收串行缓冲的数据,并分析数据包的合法性
参数:nMessageFlag:希望接收的数据包的类型 0:为主机发起的任务包 1:对加油机发起的任务的应答
pszMessage:接收数据的缓冲
返回:接收的数据包的长度,0xFF:为接收数据包出错
说明:任务发起方发出任务以后,如果需要接收方应答,则接收方需要回应和发起方
相同的包标志,加密控制则无所谓
根据帧控制字的第二个字节判断是否采用密文传递
b0~b1 0:数据包是否采用加密传送 1:数据包采用明文传递
b2~b3 00: 采用简单取反的方式加密
01:采用简单和基础密钥异或的方式进行加密数据
10:采用DES加密
11:采用3DES加密
*/
unsigned char CTestRFCardDlg::HOST_GetMessage(HANDLE handlePort,unsigned char nMessageFlag,unsigned char* pszMessage)
{
unsigned char pszBuffer[256];
unsigned char nCheck,nLength;
unsigned char nEncryptTag,nCount=0;
int nData;
do
{//Detect The Packet Head
nData = SSI_GetRFReaderByte(handlePort);
if (30 == nCount++)return SHTP_ERR;
}while(SHTP_STX != nData);
pszBuffer[0] = SHTP_STX;
//接收帧控制字:第一个字节
if(0>(nData = SSI_GetRFReaderByte(handlePort)))return SHTP_ERR;
pszBuffer[1] = nData;
nCheck = pszBuffer[0]^pszBuffer[1];
//对方发送过来的数据包的帧控制字
if(0 == (nMessageFlag&0xf0))
{//判断数据包的流水是否一致,不一致则发生帧同步错
if(g_nHostID != nData)return SHTP_ERR;
}
else
g_nReaderID = nData;
//帧控制字第二个字节
if(0>(nData = SSI_GetRFReaderByte(handlePort)))return SHTP_ERR;
pszBuffer[2] = nData;
nEncryptTag = pszBuffer[2];
nCheck ^= nEncryptTag;
//取包长度,数据包的长度不会为包引导符,如果有可能同的话,可以修改包引导符
if(0>(nData = SSI_GetRFReaderByte(handlePort)))return SHTP_ERR;
pszBuffer[3] = nData;
nLength = pszBuffer[3];
nCheck ^= nLength;
//简单判断一下长度是否合法,防止非法数据超出缓冲边界
if(0xFB < nLength) return SHTP_ERR;
//开始取数据
for(nCount=0;nCount<nLength;nCount++)
{
if(0>(nData = SSI_GetRFReaderByte(handlePort)))return SHTP_ERR;
pszMessage[nCount] = nData;
//检测数据包中的数据是不是有可能是包转义字符,这个版本不做检查,下一个版本做
nCheck ^= pszMessage[nCount];
}
//接收校验位
if(0>(nData = SSI_GetRFReaderByte(handlePort)))return SHTP_ERR;
//检查校验位,防止包引导符出现,如果出现处理为包引导符的高低位颠倒:如0x9B为0xB9
if(SHTP_STX == nCheck)
nCheck = 0xB9;
if(nCheck == nData)
{//根据帧控制字的第二个字节判断数据是否加密
if(nEncryptTag&0x03)
{//采用密文传递
nLength = DecryptMessage(pszMessage,nLength,nEncryptTag);
return nLength;
}
else
{//采用明文传递
return nLength;
}
}
return SHTP_ERR;
}
/*
功能:发送串行缓冲的数据,并分析数据包的合法性
参数:nMessageFlag:希望接收的数据包的类型
b4~b7 0:为主机发起的任务包 1:对加油机发起的任务的应答
b0~b1 0:数据包是否采用加密传送 1:数据包采用明文传递
b2~b3 00: 采用简单取反的方式加密
01:采用简单和基础密钥异或的方式进行加密数据
10:采用DES加密
11:采用3DES加密
pszMessage:接收数据的缓冲
返回:接收的数据包的长度,0xFF:为接收数据包出错
*/
//---------------------------------------------------------------------------
unsigned char CTestRFCardDlg::HOST_PutMessage(HANDLE handlePort,unsigned char nMessageFlag,const unsigned char* pszMessage,unsigned char nDataLength)
{
unsigned char uCheck;
unsigned char pszBuffer[256];
unsigned char pszData[256];
unsigned char nCount,nLength=0;
unsigned char nMessageLength;
int nResult;
//数据包的流水增加一
g_nHostID++;
g_nHostID &= 0x0F;
//组织数据缓冲,置包引导符
pszBuffer[nLength++] = SHTP_STX;
//置帧序列号
if(nMessageFlag&0xf0)
{//应答包,则直接回送上一次接收的帧序列号
pszBuffer[nLength++] = g_nReaderID;
uCheck = SHTP_STX^g_nReaderID;
}
else
{//本机发起的任务包
pszBuffer[nLength++] = g_nHostID;
uCheck = SHTP_STX^g_nHostID;
}
//发送第二帧控制字
pszBuffer[nLength++] = nMessageFlag;
uCheck ^= nMessageFlag;
//分析数据是否需要进行加密传输
memcpy(pszData,pszMessage,nDataLength);
if(nMessageFlag&0x03)
{//要进行密文传送
nMessageLength = EncryptMessage(pszData,nDataLength,nMessageFlag);
}
else
{//明文传送
nMessageLength = nDataLength;
}
//数据包的长度
pszBuffer[nLength++] = nMessageLength;
uCheck ^= nMessageLength;
//发送数据包的内容
for(nCount=0;nCount<nMessageLength;nCount++)
{//检测数据包中的数据是不是有可能是包转义字符,这个版本不做检查,下一个版本做
pszBuffer[nLength++] = pszData[nCount];
uCheck ^= pszData[nCount];
}
//检查校验位,防止包引导符出现,如果出现处理为包引导符的高低位颠倒:如0x9B为0xB9
if(SHTP_STX == uCheck)
uCheck = 0xB9;
//发送包校验字节
pszBuffer[nLength++] = uCheck;
//发送整个数句包
nResult = SSI_SendStringEx(handlePort,pszBuffer,nLength);
if(nResult)
return SHTP_ERR;
return SHTP_SUCC;
}
//---------------------------------------------------------------------------
/*
功能:对输入的数据进行加密,明文密文采用同一个缓冲是为了照顾单片机的程序(那部分堆栈有限)
参数:pszMessage待加密的数据同样也是密文的输出缓冲
nMessageLength为待加密的数据的长度
nFlag:为采用的加密的算法标志
b2~b3 00: 采用简单取反的方式加密
01:采用简单和基础密钥异或的方式进行加密数据
10:采用DES加密
11:采用3DES加密
返回:为加密后的数据长度
*/
unsigned int CTestRFCardDlg::EncryptMessage(unsigned char* pszMessage,unsigned char nMessageLength,unsigned char nFlag)
{
unsigned char nCount,nLength,nIndex;
unsigned char pszKey[16];
if(0x00 == (nFlag&0x0C))
{//简单取反的方式
for(nCount=0;nCount<nMessageLength;nCount++)
pszMessage[nCount] = ~pszMessage[nCount];
return nMessageLength;
}
if(0x04 == (nFlag&0x0C))
{//简单和基础密钥异或的方式进行加密数据
//取密钥,需要进行密钥管理,暂时采用简单的方法
memcpy(pszKey,"HengShan",8);
//数据进行8的边界对齐,需要后补0
nLength = 8-nMessageLength%8;
memset(pszMessage+nMessageLength,0x00,nLength);
for(nCount=0;nCount<(nMessageLength+nLength)/8;nCount++)
{
for(nIndex=0;nIndex<8;nIndex++)
{
pszMessage[nCount*8+nIndex] = pszMessage[nCount*8+nIndex]^pszKey[nIndex];
}
}
return nMessageLength+nLength;
}
if(0x08 == (nFlag&0x0C))
{//采用DES加密
//取密钥,需要进行密钥管理,暂时采用简单的方法
memcpy(pszKey,"HengShan",8);
//数据进行8的边界对齐,需要后补0
nLength = 8-nMessageLength%8;
memset(pszMessage+nMessageLength,0x00,nLength);
for(nCount=0;nCount<(nMessageLength+nLength)/8;nCount++)
{
//OIL_DES(pszMessage+nCount*8,pszMessage+nCount*8,pszKey,DES_ENCRYPT);
}
return nMessageLength+nLength;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -