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

📄 comm.c.svn-base

📁 keilC底层软件,能够结合硬件产生加解密密文,具有安全保护功能.
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
#include <reg52.h>
#include "c51.h"
#include "data.h"
#include "absacc.h"

typedef struct 
{
	uchar person_no;
	uchar person_pwd[10];
} person_password;

void ZDHDisplayString(uchar Page,uchar ClearFlag,CommInfx *StringPointer);
void SystemPowerCheck(void);
void RemoveNoNumber(ucharx *point);			//去除非数字数据

//输入参数为0x00返回当前可写的帐号 否则返回当前帐号数目
uchar GetFreeAcctStart(uchar Kind)					
{
	uint Address;
	uchar counter;
	ucharx TempByte;
	uchar AccountNum;

	YJHReadFlashToExterMem(AccountPage,1,AccountAreaAdd,(ucharx *)(&TempByte));

	//未开户
	if(TempByte!=0x55) 
		return(AccountNoInit);
	
	AccountNum=0;
	Address=AccountAddress;
	for(counter=0;counter<AccontNumber;counter++)
	{
		YJHReadFlashToExterMem(AccountPage,1,(ucharx *)Address,(ucharx *)(&TempByte));

		//返回可写账号
		if((Kind==0)&&(TempByte!=0x55)) 
			return(counter);	
		else if(TempByte==0x55) 
			AccountNum++;
		
		Address+=OneCountLength;
	}

	//账号已满
	if((Kind==0)&&(AccountNum>=AccontNumber)) 
		return(AccountFull);	

	//返回账号总数
	else 
		return(AccountNum);
}

//查某一账号是否存在,1:存在,0:不存在
uchar	IsAcctExist(ucharx *AccPointer)
{
	uchar counter;
	ucharx TempByte;
	uint Address;
	ucharx TempBuffer[AccountLength];

	for (counter=0;counter<AccontNumber;counter++)
	{
		Address=AccountAddress+OneCountLength*counter;
		YJHReadFlashToExterMem(AccountPage,1,(ucharx *)Address,(ucharx *)(&TempByte));
		if(TempByte==0x55)
		{
			//读取账号
			YJHReadFlashToExterMem(AccountPage,
				AccountLength,
				(ucharx *)(Address+1),
				(ucharx *)(&TempBuffer[0]));

			//比较账号
			TempByte=XramStringCompare(AccPointer,(ucharx *)(&TempBuffer[0]),AccountLength);

			//账号存在则返回
			if(TempByte==0) 
				return(counter);
		}
	}
	
	return(0xff);							//不存在
}


void SystemPowerCheck(void)
{
	uchar counter,counter1;

	YJHSaveWindow(0, 0, 127, 127, &LCD_Bffer2[0]);
	PublicBitVal=PublicBitVal|0x01;		//使能电压检测
	
	for(counter=20;counter>0;counter--)
	{
		TMOD=0x11;
		TH1=0xff;			//50us
		TL1=0xd1;
		IE=0x8a;
		counter1=100;
		while(counter1--);
	}
	ET1=0;
	RestoreWindow((ucharx *)&LCD_Bffer2[0]);
	BaudRateSet(0);
}

void OnlineCommunication(void)
{
	//用于通讯是确定数据包的长
	ucharc PacketLength[]=      
	{	0,	//READ_SN
		16,	//GEN_KEYPAIR
		33,	//LOAD_AK
		16,	//DEL_ACCU
		112,//ADD_SIG
		8,	//UNLOCK
		8,	//DIST_MACH
		48,	//CONN_CALU
		15,	//PasswordCheck
		56	//??
	};

	uchar counter,PacketSerialNum,CommandType;
	uchar flag,i,person;
	uchar PacketFlag=0;
	uchar RAM16;		
	uchar LenH;
	uchar LenL;
	uchar NewNo,temp;
	ucharx ReceiveBuff[250];
	ucharx SendBuff[250];	
	ucharx TempAcc[250];
	ucharx TempAcc1[250];
	ucharx OldBuff[250];
	ucharx tempx;	       
	uchar IsDownSK=0;
	
	////////////////
	uint  tempint;
	ucharx TempAccx[250];
	ucharx TempAccx1[250];
	uchar countera,counter1,sq_flag,pwd_flag;
	uchar pwd_type=0;
	
	person_password  xdata comm_pwd[10]
	ucharx TempBuffer1[sizeof(CommStruct)];
	CommStruct xdata  *Tpointer=&TempBuffer1[0];

	Tpointer->X1=1;
	Tpointer->Y1=45;
	Tpointer->X2=127;
	Tpointer->Y2=127;
	Tpointer->ValidItem=0;
	Tpointer->LineInterval=4;
	CommunityPcMma();

	flag=1;
	YjsWaitKeyOn(10,0);			//
	while(flag!=0)
	{
		flag=WaitKeyOff(10);
		PowerLowDec();		//电池电压检测
		if(DBYTE[PublicBitVal]&0x02!=0) SystemPowerCheck();	//电压低
	}
	BaudRateSet(0);
	
	//--------degub--------
	EA=0;			
	//--------end debug-----
	ControlStatus=ControlStatus&_Bit_SSX_Rst;   //on ssx
	ChangeStatus();
	RAM16=0xFF;
	while(1)
	{	
		//返回“SEQ+TAG+LEN1+LEN2+DATA+LRC”
		counter=ReceiveFrame(ReceiveBuff);

		//user exit(pressed enter key)
		if(counter==UserExitKey)
			break;
		else if(counter==PowerLower) 
			SystemPowerCheck();	//电压低

		NewNo=ReceiveBuff[0];    
		CommandType=ReceiveBuff[1];

		//NAK 接收不正确
		if(counter!=0)
		{
			SendBuff[0]=ReceiveBuff[0];
			SendBuff[1]=CommandType;
			SendBuff[2]=0;
			SendBuff[3]=1;
			LenH=0;           //length MSB
			LenL=1;           //length LSB
			SendBuff[4]=counter;
			flag=1;
		}

		//ACK 接收正确
		else
		{
			if((PacketSerialNum!=NewNo)||(PacketFlag==0))
			{
				PacketFlag=0x01;

				//serial number at DBYTE 0x50
				PacketSerialNum=NewNo;          

				if((CommandType!=LOAD_AK)&&(CommandType!=READ_SN))
				IsDownSK=0;
				//zdh2 modified start

				counter=PacketLength[CommandType-0x31];
				if(CommandType==0x64) 
					counter=PacketLength[8];
				
				if(CommandType==0x65) 
					counter=PacketLength[9];
				
				if(ReceiveBuff[3]!=counter)
				{
					LenH=0;	//length MSB
					LenL=1;	//length LSB
					SendBuff[4]=0x10;
					counter=CommandType;
					CommandType=0xff;
				}


				switch(CommandType)
				{
				
				//返回“Return_Code+MACH_ID+CHIP_SN=1+5+4(字节)”
				case READ_SN:                       
				{
					SendBuff[4]=0x00;

					YJHReadFlashToExterMem(ConfigFilePage,5,(ucharx *)MmqbhAdd,&SendBuff[5]);
					LenH=0;//length MSB
					LenL=6;//length LSB
					break;
				}

				//生成密钥对
				case GEN_KEYPAIR:           
				{
					LenH=0;   //length MSB
					LenL=97;  //length LSB

					counter=GetFreeAcctStart(0x00);         //get acc num
					if((counter==AccountNoInit)||(counter==AccountFull))
					{
						SendBuff[4]=counter;                 //0x63;
						break;
					}					

					memcpy(&TempAcc[2],&ReceiveBuff[4],16);
					RemoveNoNumber(&TempAcc[2]);

					counter=IsAcctExist(&TempAcc[2]);                  //帐号已存在

					if(counter!=0xff)
					{
						SendBuff[4]=AccountExist;
						break;
					}

					memcpy(&TempAcc1[1],&TempAcc[2],16);

					TempAcc[0]=GenerateDisKey;
					TempAcc[1]=16;					


					counter=EncryptorAccess(TempAcc);

					TH1=0xfd;
					TL1=0xfd;
					if((counter==0)&&(TempAcc[2]==0x00))
					{
						TempAcc1[0]=0x55;						
						memcpy(&TempAcc1[18],&TempAcc[3+16+80],48);
						memcpy(&SendBuff[5],&TempAcc[3],96);	
					}
					else
					{
						SendBuff[4]=TempAcc[2];
						break;
					}					
					SendBuff[4]=0;
					IsDownSK=1;
					break;
				}

				//下载AK支
				case LOAD_AK:                           
				{           
					///////////zdh added
					LenH=0;   //length MSB
					LenL=1;  //length LSB
					if(!IsDownSK)
					{
						SendBuff[4]=0x24;
						break;
					}
					IsDownSK=0;

					//得到帐号
					memcpy(&TempAcc[0],&ReceiveBuff[4],33);
					//去掉非字母
					RemoveNoNumber(&TempAcc[0]);
					//查看帐号是否存在,如果存在返回错误
					if(IsAcctExist(&TempAcc[0])!=0xff)
					{
						SendBuff[4]=AccountExist;
						break;
					}
					else
					{
						counter=memcmp(&TempAcc1[1],&TempAcc[0],16);
						SendBuff[4]=AccDiff;       //帐号不存在
						if(counter==0x00)			
						{   
							TempAcc1[17]=TempAcc[16];
							memcpy(&TempAcc1[18+48],&TempAcc[17],16);																
							TempAcc1[18+48+16]=32;
							counter=GetFreeAcctStart(0x00);
							AddCheckSum(&TempAcc1[1],16+1+48+16+1);
							ZDHWriteFlash(AccountPage,
								84,
								&TempAcc1[0],
								AccountAddress+OneCountLength*counter);
							
							ZDHWriteFlash(AccountPage,
								84,&TempAcc1[0],
								AccountBakAdd+10+OneCountLength*counter);
							AddPersonAcc(0,0,counter);								
							AddPersonAcc(3,0,counter);					//zdhy 2002.01.09
							SendBuff[4]=0x00;
						}
					}
					break;
				}

				case DEL_ACCU:              //删除账号 ADD_SIG://
				{
					LenH=0;   //length MSB
					LenL=1;   //length LSB
					memcpy(TempAcc,&ReceiveBuff[4],16);
					RemoveNoNumber(TempAcc);
					counter=IsAcctExist(TempAcc);         //SEQ+TAG+LEN1+LEN2+DATA+LRC

					if(counter!=0xff)
					{
						tempx=0x00;
						ZDHWriteFlash(AccountPage,
							1,
							&tempx,
							AccountAddress+OneCountLength*counter);
						
						FlashWriteEnable=Enable;
						EraseBlock(BatchBufferPage,TempRecordAdd/0x1000);
						LineAcct();
						SendBuff[4]=0x00;
					}
					else
					{
						SendBuff[4]=0x26;
					}
					break;
				}

				case ADD_SIG:               //增发签名 DEL_ACCU://
				{
					LenH=0;   //length MSB
					LenL=9;   //length LSB

					memcpy(&TempAcc[2],&ReceiveBuff[4],16+16+80);

					RemoveNoNumber(&TempAcc[2]);
					counter=IsAcctExist(&TempAcc[2]);  //SEQ+TAG+LEN1+LEN2+DATA+LRC
					if(counter==0xFF)
					{
						SendBuff[4]=0x26;
						break;
					}
					temp=counter;
					YJHReadFlashToExterMem(AccountPage,
						48,
						(ucharx *)(AccountAddress+OneCountLength*temp+18),
						&TempAcc1[2]);

					TempAcc1[0]=ReadSK;
					TempAcc1[1]=48;
					counter=EncryptorAccess(TempAcc1);
					if(counter!=0)       //读入签名密钥SK
					{
						SendBuff[4]=counter;
						break;
					}

					YJHReadFlashToExterMem(AccountPage,
						16,
						(ucharx *)(AccountAddress+OneCountLength*temp+18+48),
						&TempAcc1[2]);
					TempAcc1[0]=ReadAK;
					TempAcc1[1]=16;
					counter=EncryptorAccess(TempAcc1);
					if(counter!=0)
					{
						SendBuff[4]=counter;
						break;
					}


					TempAcc[0]=AddEncrypt;
					TempAcc[1]=112;
					counter=EncryptorAccess(TempAcc);
					SendBuff[4]=counter;
					memcpy(&SendBuff[5],&TempAcc[3],8);
					break;
				}

				//----------------------------------------------------------
				case UNLOCK:                //解锁
				{
					LenH=0;   //length MSB
					LenL=1;   //length LSB

					counter=0;
					for(i=0;i<8;i++)
					{
						YJHReadFlashToExterMem(AccountPage,1,(ucharx *)(AccountAreaAdd+1+i),&tempx);	

						if(ReceiveBuff[4+i]!=tempx)
						{
							counter=1;
						}
					}

					if(counter)
					{
						SendBuff[4]=0x27;
					}
					else
					{
						UnlockPass(3,0);
						SendBuff[4]=0x00;
					}
					break;
				}


				case DIST_MACH:             //发行机具
				{
					LenH=0;   //length MSB
					LenL=1;   //length LSB
					counter=GetFreeAcctStart(0x01);

					if((counter!=0)&&(counter!=0x63))
					{
						SendBuff[4]=0x23;
						break;
					}
					
					FlashWriteEnable=Enable;
					EraseBlock(AccountPage,AccountAreaAdd/0x1000);
					TempAcc[0]=0x55;
					memcpy(&TempAcc[1],&ReceiveBuff[4],8);

					ZDHWriteFlash(AccountPage,9,TempAcc,AccountAreaAdd);
					ZdhFlashChecksum(AccountPage,AccountAreaAdd+1,8);

					ZDHWriteFlash(AccountPage,9,TempAcc,AccountBakAdd);
					ZdhFlashChecksum(AccountPage,AccountBakAdd+1,8);		//账号备份区
					ClearSomeData();
					SendBuff[4]=0x00;
					break;
				}

				////////////////////////////////////////////////////////////////////////////////
				//////////////////////          
				case CONN_CALU:             //联机计算支付密码
				{
					LenH=0;   //length MSB
					LenL=11;  //length LSB
					NowMM.Nomoney=1;
					SendBuff[4]=0;
					if(pwd_type==0x00)
					{
						SendBuff[4]=0x62;
						pwd_type=0;
						break;
					}
					memcpy(TempAcc1,&ReceiveBuff[4],48);						
					counter1=IsAcctExist(&TempAcc1[15]);         //SEQ+TAG+LEN1+LEN2+DATA48 =52
					if(counter1==0xFF)//帐号不存在
					{
						SendBuff[4]=0x26;
						pwd_type=0;
						break;
					}

					memcpy(&(NowMM.BcdAccount[0]),&TempAcc1[15],33);
					NowMM.qfman=comm_pwd[0].person_no;				//签发
					NowMM.shman=comm_pwd[1].person_no;				//审核
					NowMM.sqman=comm_pwd[2].person_no;				//授权

					if((comm_pwd[0].person_no)==0xff)
					{	
						SendBuff[4]=0x55;				// 	no qfy
						pwd_type=0;
						break;
					}

					sq_flag=IsLessThenNoPerMoney();


					//////////////////////check     qfy shy   password//////////////////////
					countera=2;
					if((sq_flag==2)&&(pwd_type==1)) 
						countera=3;

					for(counter=0;counter< countera;counter++)
					{
						if(((counter==1)||(counter==2))&&\
							(ReadOneXByte(ConfigFilePage,(ucharx*)(PersonArbi+5+(counter*5)))!=1))
							continue;
						
						person=comm_pwd[counter].person_no;
						tempint=ConfigFileAdd+FirstPersonAdd+(counter*16+person)*OnePersonLength;
						if((ReadOneXByte(ConfigFilePage,(ucharx *)tempint)!=0x55)||(person==0xff))
						{
							SendBuff[4]=0x55+counter*4;		//无此人员
							break;
						}
						
						//检查人员对该帐号是否有权限,AccountNum帐号代码,返回1有 0无
						pwd_flag=IsAccountPermit(counter,comm_pwd[counter].person_no,counter1,0,(ucharx *)0);

						//person  no permit
						if(!pwd_flag)		
						{
							SendBuff[4]=0x54+counter*4;				
							break;
						}
						//口令被锁
						if(ExamPass((ucharx *) &(comm_pwd[counter].person_pwd[0]),0,person)==0xff)
						{
							SendBuff[4]=0x53+counter*4;			
							break;
						}
						//口令错			
						if(ExamPass((ucharx *) &(comm_pwd[counter].person_pwd[0]),0,person)!=PasswordPass)
						{
							SendBuff[4]=0x52+counter*4;			
							break;
						}			
					}
					
					if(SendBuff[4])
					{
						pwd_type=0;
						break;
					}
					/////////////////////////////////end of qfy    shy   password check//////
					/////////////////////////////////check sqy password/////////////////////////////
					if(sq_flag==2)
					{
						if(pwd_type==1)           //password cmd
						{
							//授权金额判断
							if(IsMoreThenPermit(&NowMM.BcdMoney[0],comm_pwd[2].person_no))	
							{

								SendBuff[4]=0x5e;				//   >  sqy money
								pwd_type=0;
								break;
							}

⌨️ 快捷键说明

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