⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mifare.c

📁 以上几个手持机的初始化程序
💻 C
📖 第 1 页 / 共 5 页
字号:
		for(i=0;i<3;i++)
		{
			gcSector = gstWorkCardInfomation.stCardStruct.cMoneySector;
			if(AuthenCard(&glSerialNo,gcSector,MIF_WRITE))
				if(CardMFCValueWithTransfer(cMode, blck, blck, lMoney)==MI_OK)
					break;
		}
		if (i==3) return 0;
		break;
	case 33:
	case 34:
		for(i=0;i<3;i++)
		{
			gcSector = gstWorkCardInfomation.stCardStruct.cEveTimesSector;
			if(AuthenCard(&glSerialNo,gcSector,MIF_WRITE))
				if(CardMFCValueWithTransfer(cMode, blck, blck, lMoney)==MI_OK)
					break;
		}
		if (i==3) return 0;
		break;
	default:
		return 0;
	}
	return 1;
}
//更改指针
unsigned char ModifyPoint()
{
	unsigned char  i;

	gcSector = gstWorkCardInfomation.cHisCharge / 3;
	gcBlock = gstWorkCardInfomation.cHisCharge % 3;
	gcSector = gstWorkCardInfomation.stCardStruct.cHistorySector[gcSector];
	gcBlock += gcSector * 4;

	memcpy(gcMifBuffer, gcNowTime+3, 4);		
//	memcpy(gcMifBuffer+4, &gstWorkCardInfomation.lMoney, 4);
	OverTurnString(gcMifBuffer+4, &gstWorkCardInfomation.lMoney, 4);
//	memcpy(gcMifBuffer+8, &glChargeMoney, 3);
	OverTurnString(gcMifBuffer+8, (unsigned char*)&glChargeMoney+1, 3);
	gcMifBuffer[11] = gcChargeType;
	memcpy(gcMifBuffer+12, gcPSAMNo, 4);

	if(!AuthenCard(&glSerialNo, gcSector, MIF_WRITE)) return 0;
	for(i=0;i<3;i++)
		if(CardMFCWrite16Bytes(gcBlock, gcMifBuffer)==MI_OK) break;
	if(i==3) return 0;
	if (gstWorkCardInfomation.cHisCharge < 8)
		gstWorkCardInfomation.cHisCharge ++;
	else
		gstWorkCardInfomation.cHisCharge = 0;

	switch(gcChargeType)
	{
	case CHARGETYPE_DECMONEY:
	case CHARGETYPE_INCMONEY:
	case CHARGETYPE_PURMONEY:
		if (gstWorkCardInfomation.iMoneyTotleTimes < 0xffff)		
			gstWorkCardInfomation.iMoneyTotleTimes ++;
		else
			gstWorkCardInfomation.iMoneyTotleTimes = 0;
		break;
	case CHARGETYPE_DECTIMES:
	case CHARGETYPE_INCTIMES:
	case CHARGETYPE_PURTIMES:
		if (gstWorkCardInfomation.iTimesTotleTimes < 0xffff)		
			gstWorkCardInfomation.iTimesTotleTimes ++;
		else
			gstWorkCardInfomation.iTimesTotleTimes = 0;
		break;
	}
	gcSector = gstWorkCardInfomation.stCardStruct.cPublicSector;
	gcBlock = gcSector * 4;
	if(!AuthenCard(&glSerialNo, gcSector, MIF_WRITE)) return 0;
	for(i=0;i<3;i++)
		if(CardMFCRead16Bytes(gcBlock, gcMifBuffer)==MI_OK) break;
	if(i==3) return 0;

	gcMifBuffer[0] = gstWorkCardInfomation.cHisCharge+1;		
	switch(gcChargeType)
	{
	case CHARGETYPE_DECMONEY:
	case CHARGETYPE_INCMONEY:
	case CHARGETYPE_PURMONEY:
//		memcpy(gcMifBuffer+1, &gstWorkCardInfomation.iMoneyTotleTimes, 2);
		OverTurnString(gcMifBuffer+1, &gstWorkCardInfomation.iMoneyTotleTimes, 2);
		break;
	case CHARGETYPE_DECTIMES:
	case CHARGETYPE_INCTIMES:
	case CHARGETYPE_PURTIMES:
//		memcpy(gcMifBuffer+4, &gstWorkCardInfomation.iTimesTotleTimes, 2);
		OverTurnString(gcMifBuffer+4, &gstWorkCardInfomation.iTimesTotleTimes, 2);
		break;
	}
	gcMifBuffer[15] = 0;
	for(i=0;i<15;i++)
		gcMifBuffer[15] ^= gcMifBuffer[i];

	for(i=0;i<3;i++)
		if(CardMFCWrite16Bytes(gcBlock, gcMifBuffer)==MI_OK) break;
	if(i==3) return 0;
	return 1;
}
//分析卡片目录结构
void AnalyseCardStruct(unsigned char *CardDir)
{
	unsigned char i, j;
	
	j = 0;
	memset((unsigned char*)&gstWorkCardInfomation.stCardStruct, 0xff, 9);
	for(i=0;i<16;i++)
	{
		switch(CardDir[i])
		{
		case 0x00:
			gstWorkCardInfomation.stCardStruct.cDirSector = i;
			break;
		case 0x01:
			gstWorkCardInfomation.stCardStruct.cPublishSector = i;
			break;
		case 0x10:
			gstWorkCardInfomation.stCardStruct.cMoneySector = i;
			break;
		case 0x03:
			gstWorkCardInfomation.stCardStruct.cHistorySector[j] = i;
			j ++;
			break;
		case 0x06:
			gstWorkCardInfomation.stCardStruct.cPublicSector = i;
			break;
		case 0x07:
			gstWorkCardInfomation.stCardStruct.cOddTimesSector = i;
			break;
		case 0x08:
			gstWorkCardInfomation.stCardStruct.cEveTimesSector = i;
			break;
		default:
			break;
		}
	}
}
//获取卡片密钥
unsigned char GetCardKey(unsigned long lSerialNo)
{
	unsigned char i, buf[50], outbuf[50], CardKeyIndex;
	if ((lSerialNo == gstWorkCardKey.lSerialNo)&&(gcGetKeyFinished == 4))
		return 1;
//	clr_scr();
//	move_cursor(0,0);	
//	puts("GetCardKey");
//	getch();	
	gstWorkCardInfomation.cKeyVerision = 0;
	memcpy(gstWorkCardKey.cKeyA[0], gcDefaultKeyA, 6);
	memcpy(gstWorkCardKey.cKeyB[0], gcDefaultKeyB, 6);
	gcGetKeyFinished = 0;
	if(CardMFCAuthKey(PICC_AUTHENT1A, (unsigned char*)&lSerialNo, gstWorkCardKey.cKeyA[0], 3)==MI_OK)
	{
		CardMFCRead16Bytes(1,gcMifBuffer);

		AnalyseCardStruct(gcMifBuffer);
		gstWorkCardInfomation.cGetInfoFinished = SECTOR_DIR;
	}
	else
	{
		if (CardTypeARequest(PICC_REQIDL, gcATQ)!=MI_OK) return 0;
		if (CardTypeASelect(PICC_ANTICOLL1, (unsigned char*)&lSerialNo, gcATS)!=MI_OK) return 0;
		memcpy(gstWorkCardKey.cKeyA[0], gcDefaultKeyAf, 6);
		memcpy(gstWorkCardKey.cKeyB[0], gcDefaultKeyBf, 6);
		if(CardMFCAuthKey(PICC_AUTHENT1A, (unsigned char*)&lSerialNo, gstWorkCardKey.cKeyA[0], 3)==MI_OK)
		{
			gstWorkCardInfomation.cKeyVerision = 0xff;
			//for debug
//			CardMFCRead16Bytes(1,gcMifBuffer);
//			AnalyseCardStruct(gcMifBuffer);
//			gstWorkCardInfomation.cGetInfoFinished = SECTOR_DIR;
			//for debug
		}
		else
		{
			return 0;
		}
	}
	
	//for debug
	//gstWorkCardInfomation.cKeyVerision==0xff;	
	//for debug
	if(gstWorkCardInfomation.cKeyVerision!=0xff)
	{
		memcpy(gcKeyA1, (unsigned char*)&lSerialNo, 4);
		gcKeyA1[4] = gcKeyA1[0];
		gcKeyA1[5] = gcKeyA1[1];
		memcpy(gstWorkCardKey.cKeyA[1], gcKeyA1, 6);
		memcpy(gstWorkCardKey.cKeyB[1], gcKeyB1, 6);
		gcGetKeyFinished = 1;

		if(AuthenCard(&lSerialNo, 1, MIF_READ))
		{
			CardMFCRead16Bytes(4,gcMifBuffer);

			gstWorkCardInfomation.cKeyVerision = gcMifBuffer[14];
			for(i=0;i<gcKeyMarkSetNum;i++)
			{
				if(gstWorkCardInfomation.cKeyVerision == gstKeyMarkSet[i].KeyVer)
				{
					gstWorkCardInfomation.cCardKeyIndex = i;
					CardKeyIndex = i;
					break;
				}
			}
			if(i==gcKeyMarkSetNum)
			{
				gstWorkCardInfomation.cKeyVerision = 0xA0;
			}
		}
		else
			gstWorkCardInfomation.cKeyVerision = 0xA0;
	}
	if ((gstWorkCardInfomation.cKeyVerision==0xff)||(gstWorkCardInfomation.cKeyVerision==0xA0))
	{
		for(i=1;i<16;i++)
		{
			memcpy(gstWorkCardKey.cKeyA[i], gstWorkCardKey.cKeyA[0], 6);
			memcpy(gstWorkCardKey.cKeyB[i], gstWorkCardKey.cKeyB[0], 6);
		}
		gstWorkCardInfomation.cKeyVerision = gstWorkCardKey.cKeyA[0][0];
		gcGetKeyFinished = 4;

	}
	else
	{
		switch(gcGetKeyFinished)
		{
		case 0:		
			gcGetKeyFinished = 1;
		case 1:		
			gcSector = gstWorkCardInfomation.stCardStruct.cPublishSector;
			gcBlock = gcSector * 4;
			AuthenCard(&lSerialNo, gcSector, MIF_READ);
			CardMFCRead16Bytes(gcBlock, gcMifBuffer);
			memcpy(buf, gcMifBuffer, 2);
			memcpy(buf+2, (unsigned char*)&lSerialNo, 4);
			memcpy(buf+6, gcMifBuffer+4, 2);	
			memcpy(buf+8, gcMifBuffer+8, 4);	
			buf[12] = 0x02;
			buf[13] = 0x03;
			buf[14] = 0x07;

			i = GetMifKey(gstKeyMarkSet[CardKeyIndex].Key02A, gstKeyMarkSet[CardKeyIndex].KeyCardMAC, buf, 15);

			if(i != 18)
			{

				return 0;
			}
			delay_n_ms(2);
			if(!GetResponse(outbuf, i))
			{
				return 0;
			}
			delay_n_ms(2);
			buf[12] = 0x02;
			buf[13] = 0x07;
			i = GetMifKey(gstKeyMarkSet[CardKeyIndex].Key02B, gstKeyMarkSet[CardKeyIndex].KeyCardMAC, buf, 14);
			if(i != 12)
			{
				return 0;
			}
			delay_n_ms(2);
			if(!GetResponse(outbuf+20, i))
			{
				return 0;
			}
			memcpy(gstWorkCardKey.cKeyA[2], outbuf,6);
			memcpy(gstWorkCardKey.cKeyB[2], outbuf+20, 6);
			memcpy(gstWorkCardKey.cKeyA[6], outbuf,6);
			memcpy(gstWorkCardKey.cKeyB[6], outbuf+20, 6);
			gcGetKeyFinished = 2;
		case 2:		
			for(i=3;i<6;i++)
			{
				memcpy(gstWorkCardKey.cKeyA[i], outbuf+6, 6);
				memcpy(gstWorkCardKey.cKeyB[i], outbuf+6, 6);
			}
			gcGetKeyFinished = 3;
		case 3:		
			memcpy(gstWorkCardKey.cKeyA[7], outbuf+12, 6);
			memcpy(gstWorkCardKey.cKeyB[7], outbuf+26, 6);
			memcpy(gstWorkCardKey.cKeyA[8], outbuf+12, 6);
			memcpy(gstWorkCardKey.cKeyB[8], outbuf+26, 6);
			gcGetKeyFinished = 4;
		}
	}
	gstWorkCardKey.lSerialNo = lSerialNo;
	gstWorkCardInfomation.lSerialNo = lSerialNo;
	return 1;
}
//获取卡片信息
unsigned char GetCardInformation(unsigned long lSerialNo, unsigned char cSectorFlag)
{
	unsigned char  i, sw, j;
	unsigned long  l1, l2;

/*	set_console_english();
	clr_scr();
	Dispbcd(0,0,&gstWorkCardInfomation.stCardStruct,7);
	Dispbcd(0,2,&lSerialNo,4);
	Dispbcd(0,3,&gstWorkCardKey.lSerialNo,4);
	Dispbcd(0,4,&gcGetKeyFinished,1);
*/	if ((lSerialNo != gstWorkCardKey.lSerialNo)||(gcGetKeyFinished!=4))
	{
		if (!GetCardKey(lSerialNo)) return 0;
	}	
	
	if ((gstWorkCardInfomation.lSerialNo == lSerialNo)&&((gstWorkCardInfomation.cGetInfoFinished & cSectorFlag)== cSectorFlag)) return 1;
	gstWorkCardInfomation.lSerialNo = lSerialNo;
	//if ((gstWorkCardInfomation.cKeyVerision == 0xff)||(gstWorkCardInfomation.cKeyVerision == 0xA0)) return 2; for debug
	if(((cSectorFlag&SECTOR_PUBLISH)==SECTOR_PUBLISH)&&((gstWorkCardInfomation.cGetInfoFinished & SECTOR_PUBLISH)!=SECTOR_PUBLISH))
	{
//		clr_scr();
//		move_cursor(0,0);	
//		puts("cPublishSector");
//		getch();

//		gcSector = gstWorkCardInfomation.stCardStruct.cPublishSector;
//		gcBlock = gcSector * 4;
//		if (!AuthenCard(&lSerialNo, gcSector, MIF_WRITE)) return 0;
//		if (CardMFCRead16Bytes(gcBlock, gcMifBuffer)!=MI_OK) return 0;
//		gcMifBuffer[12] = 0x02;
//		if (CardMFCWrite16Bytes(gcBlock, gcMifBuffer)!=MI_OK) return 0;

		gcSector = gstWorkCardInfomation.stCardStruct.cPublishSector;
		gcBlock = gcSector * 4;
		if (!AuthenCard(&lSerialNo, gcSector, MIF_READ)) return 0;
		if (CardMFCRead16Bytes(gcBlock, gcMifBuffer)!=MI_OK) return 0;
	
		memcpy(gstWorkCardInfomation.cCityCode, gcMifBuffer, 2);						
		memcpy(gstWorkCardInfomation.cTradeCode, gcMifBuffer + 2, 2);					
		memcpy(&gstWorkCardInfomation.lCardNo, gcMifBuffer+4, 4);
		memcpy(gstWorkCardInfomation.cAuthenCode, gcMifBuffer + 8, 4);				
		gstWorkCardInfomation.cUsageFlag  = gcMifBuffer[12];							
		gstWorkCardInfomation.cCardType = gcMifBuffer[13];							
		gstWorkCardInfomation.cCardVer = gcMifBuffer[14];							
		if (CardMFCRead16Bytes(gcBlock+1, gcMifBuffer)!=MI_OK) return 0;
	
		memcpy(gstWorkCardInfomation.cNewDate, gcMifBuffer, 4);						
		memcpy(gstWorkCardInfomation.cLimitDate, gcMifBuffer + 4, 4);				
		memcpy(gstWorkCardInfomation.cUseDate, gcMifBuffer + 8, 4);					
	//	memcpy(&gstWorkCardInfomation.iPreMoney, gcMifBuffer + 12, 2);					
		OverTurnString(&gstWorkCardInfomation.iPreMoney, gcMifBuffer + 12, 2);					
		if (CardMFCRead16Bytes(gcBlock+2, gcMifBuffer)!=MI_OK) return 0;
	
		memcpy(gstWorkCardInfomation.cLastChargeDateTime, gcMifBuffer, 4);			
	//	memcpy(&gstWorkCardInfomation.lLastMoney, gcMifBuffer + 4, 4);					
		OverTurnString(&gstWorkCardInfomation.lLastMoney, gcMifBuffer + 4, 4);					
	//	memcpy(&gstWorkCardInfomation.iLastChargeMoney, gcMifBuffer + 8, 2);					
		OverTurnString(&gstWorkCardInfomation.iLastChargeMoney, gcMifBuffer + 8, 2);					
		memcpy(gstWorkCardInfomation.cLastChargeWorker, gcMifBuffer + 10, 2);		
		if(gstWorkCardInfomation.cCardType>0x20)
		{
			return 1;
		}
		gstWorkCardInfomation.cGetInfoFinished |= SECTOR_PUBLISH;
	}
	
	if(((cSectorFlag&SECTOR_PUBLIC)==SECTOR_PUBLIC)&&((gstWorkCardInfomation.cGetInfoFinished & SECTOR_PUBLIC)!=SECTOR_PUBLIC))
	{
	
//		clr_scr();
//		move_cursor(0,0);	
//		puts("cPublicSector");
//		getch();
	
		gcSector= gstWorkCardInfomation.stCardStruct.cPublicSector;
		gcBlock = gcSector * 4;
		if (!AuthenCard(&lSerialNo, gcSector, MIF_READ)) return 0;
		if (CardMFCRead16Bytes(gcBlock, gcMifBuffer)!=MI_OK) return 0;
		if ((gcMifBuffer[3]!=0x01)&&(gcMifBuffer[3]!=0x02))
		{
			if (CardMFCRead16Bytes(gcBlock+1, gcMifBuffer)!=MI_OK) return 0;
			for(i=0;i<16;i++)
				gcMifBuffer[i] = 0xff - gcMifBuffer[i];
			if (gcMifBuffer[0] < 9)
				gcMifBuffer[0] ++;
			else
				gcMifBuffer[0] = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -