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

📄 mifare.c.bak

📁 用于小商品消费的SMS消费机程序
💻 BAK
📖 第 1 页 / 共 5 页
字号:
				if(CardMFCValueWithTransfer(cMode, 34, 33, 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_READ)) 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_READ)) 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;
		glSerialNo = lSerialNo;
	}
	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];
		}
		else
			gstWorkCardInfomation.cKeyVerision = 0xA0;
	}
//	clr_scr();
//	move_cursor(0,0);
//	set_console_english();
//	Dispbcd(0,0, &gstWorkCardInfomation.cKeyVerision,1);
//	set_console_chinese();

	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);
			
			if(!SelectFile(0, 0x1001)) return 0;
			delay_n_ms(20);

			memcpy(buf, gcMifBuffer, 2);
			memcpy(buf+2, (unsigned char*)&lSerialNo, 4);
			memcpy(buf+6, gcMifBuffer+6, 2);	
			memcpy(buf+8, gcMifBuffer+8, 4);	
			buf[12] = 0x02;
			buf[13] = 0x03;
			buf[14] = 0x07;
	
//			clr_scr();
//			move_cursor(0,0);
//			set_console_english();
//			Dispbcd(0,0, buf, 16);
//			set_console_chinese();

			i = GetMifKey(1, 1, buf, 15);

			if(i != 18)
			{
				return 0;
			}
			delay_n_ms(2);
			if(!GetResponse(outbuf, i))
			{
				return 0;
			}

//			clr_scr();
//			move_cursor(0,0);
//			set_console_english();
//			Dispbcd(0,0, outbuf, 24);
//			set_console_chinese();

			delay_n_ms(2);
			memcpy(gstWorkCardKey.cKeyA[2], outbuf, 6);
			memcpy(gstWorkCardKey.cKeyA[6], outbuf, 6);
			gcGetKeyFinished = 2;
		case 2:		
			for(i=3;i<6;i++)
			{
				memcpy(gstWorkCardKey.cKeyA[i], outbuf+6, 6);
			}
			gcGetKeyFinished = 3;
		case 3:		
			memcpy(gstWorkCardKey.cKeyA[7], outbuf+12, 6);
			memcpy(gstWorkCardKey.cKeyA[8], outbuf+12, 6);
			gcGetKeyFinished = 4;
		}
	}
//	for(i=0;i<9;i++)
//	{
//		clr_scr();
//		move_cursor(0,0);
//		set_console_english();
//		printf("%d", i);
//		Dispbcd(0,1, &gstWorkCardKey.cKeyB[i], 6);
//		set_console_chinese();
//	}
//
	gstWorkCardKey.lSerialNo = lSerialNo;
	gstWorkCardInfomation.lSerialNo = lSerialNo;
	return 1;
}
/*
//获取卡片密钥
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)
	{
		//OverTurnString(gcKeyA1, (unsigned char*)&lSerialNo, 4);
		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];
		}
		else
			gstWorkCardInfomation.cKeyVerision = 0xA0;
	}
//	clr_scr();
//	move_cursor(0,0);
//	set_console_english();
//	Dispbcd(0,0, &gstWorkCardInfomation.cKeyVerision,1);
//	set_console_chinese();

	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:		
			memset(gcMifBuffer,0, sizeof(gcMifBuffer));
			gcSector = gstWorkCardInfomation.stCardStruct.cPublishSector;
			gcBlock = gcSector * 4;
			if(!AuthenCard(&lSerialNo, gcSector, MIF_READ)) return 0;
			if(CardMFCRead16Bytes(gcBlock, gcMifBuffer)!=MI_OK) return 0;
			
//			clr_scr();
//			set_console_english();
//			Dispbcd(0,0,gcMifBuffer,16);
//			move_cursor(0,4);
//			puts("SelectFile");
//			getch();
//			set_console_chinese();

			if(!SelectFile(0, 0x1001)) return 0;
			delay_n_ms(2);

			memcpy(buf, gcMifBuffer, 2);
			memcpy(buf+2, (unsigned char*)&lSerialNo, 4);
			memcpy(buf+6, gcMifBuffer+6, 2);	
			memcpy(buf+8, gcMifBuffer+8, 4);	
			buf[12] = 0x01;
			buf[13] = 0x02;
			buf[14] = 0x03;
			buf[15] = 0x07;
			buf[16] = 0x00;
	
//			clr_scr();
//			move_cursor(0,0);
//			set_console_english();
//			puts("GetMifKey");
//			Dispbcd(0,2, buf, 16);
//			set_console_chinese();

			i = GetMifKey(1, 1, buf, 17);

			if(i != 30)
			{
				return 0;
			}
			delay_n_ms(2);
			
//			clr_scr();
//			move_cursor(0,0);

⌨️ 快捷键说明

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