📄 decrypt.c
字号:
//#include <stdio.h>
//#include "stm32f10x_lib.h"
#include "Decrypt.h"
u8 KeyNumber;
u32 Decrypt_KeyH[];
u32 Decrypt_KeyL[];
u8 Learn_Key_State;
KeyID_Address KeyID0_Address ={0,1,2,3,4};
KeyID_Address KeyID1_Address ={0,1,2,3,4};
KeyID_Address KeyID2_Address ={0,1,2,3,4};
KeyID_Address KeyID3_Address ={0,1,2,3,4};
/*********************************************************************
Founction Name :Decrption()
Founction :Call this founction to decrypt the recived encrypted code,
so you can validate whether the transmitter a legal one .
if it is ,then you can take appropriate actions
Peramater :none
Output :none
Note :
***********************************************************************/
/*the wright decrypted code is 0x0121A0B5***********/
/******************0755-82996943, 陈工(生产商电话)*****************/
/*****************************************************************************************************
this function perform the decypt and learn routine depending on the condition of Learn_key.
******************************************************************************************************/
void Code_Recive_Manage(void)
{
u8 i=0,j=4;
KeyID KeyID0,
KeyID1,
KeyID2,
KeyID3;
/***************Initiate global variables*************************************/
Recived_Data.Hopping_Code =0;
Recived_Data.Serial_Number =0;
Recived_Data.ButtonAStatus =0;
/******************************************************************************/
KeyID0=Read_KeyID_From_EEP(KeyID0_Address);
KeyID1=Read_KeyID_From_EEP(KeyID1_Address);
KeyID2=Read_KeyID_From_EEP(KeyID2_Address);
KeyID3=Read_KeyID_From_EEP(KeyID3_Address);
while(1)
{
GPIOC->ODR &=0x0000;
if(Data_Recive_Finish) //code recive successed
{
if(Recived_Data.Serial_Number==KeyID0.SeriaNumber) j=0;
else if(Recived_Data.Serial_Number==KeyID1.SeriaNumber) j=1;
else if(Recived_Data.Serial_Number==KeyID2.SeriaNumber) j=2;
else if(Recived_Data.Serial_Number==KeyID3.SeriaNumber) j=3;
else j=4 ; //The transmitter is a new one that need to learn and initiate it`s record data.
// else enter alarm state;
switch(j)
{
case(0): //The transmitter is key 0 .
Decrption(KeyID0, KeyID0_Address );
Delay_Xs(1);
break;
case(1): //The transmitter is key 1.
Decrption(KeyID1,KeyID1_Address );
Delay_Xs(1);
break;
case(2): //The transmitter is key 2.
Decrption(KeyID2,KeyID2_Address );
Delay_Xs(1);
break;
case(3): // The transmitter is key 3.
Decrption(KeyID3,KeyID3_Address );
Delay_Xs(1);
break;
case(4): if( Learn_Key_State //it is a new come key to learn
&&Recived_Data.Serial_Number) //If the learned key is less than max number (here it is 4) than learn and record information of the newcome.
{
switch(i){
case(0):KeyID0=KeyGen( ); //The new comer is the first one of this system.
Write_KeyID_To_EEP(KeyID0,KeyID0_Address);
i++;
Learn_Key_State=0;
break;
case(1):KeyID1=KeyGen( ); //The second one .
Write_KeyID_To_EEP(KeyID1,KeyID1_Address);
i++;
Learn_Key_State=0;
break;
case(2):KeyID2=KeyGen( ); //The third one.
Write_KeyID_To_EEP(KeyID2,KeyID2_Address);
i++;
Learn_Key_State=0;
break;
case(3):KeyID3=KeyGen( ); //The forth one ,and in this case ,it will be the last one .
Write_KeyID_To_EEP(KeyID3,KeyID3_Address);
i=0;
Learn_Key_State=0;
break;
}
}else if (Learn_Key_State){
i=0;
KeyID0=KeyGen( ); //The new comer is the first one of this system.
i++;
Learn_Key_State=0;
}
default://alarm();
break;
}
}
Data_Recive_Finish=0; //ready to recive next key.
}
}
/*********************************************************************
Founction Name : Decrption()
Founction :you can use this founction to decrypt the recived code using pre_generated security key
when the reciver is in normal mode. And then perform the control routine according to the
statous domain of the recived code .
Peramater :none
Output :none
Note :
***********************************************************************/
void Decrption(KeyID KeyIDx,KeyID_Address KeyIDx_Address)
{
u8 ButtonAStatus;
u32 v,DecryptedCode; // decryption key pik,pin,,bitin,keybit,keybit2 // shift register
u16 i;
DecryptedCode= Recived_Data.Hopping_Code;
ButtonAStatus=Recived_Data.ButtonAStatus;
Recived_Data.Hopping_Code =0;
Recived_Data.Serial_Number =0;
Recived_Data.ButtonAStatus =0;
//low 32 bits of decrypt key
DecryptedCode=Decrypt(KeyIDx.Decrypt_KeyH,KeyIDx.Decrypt_KeyL, DecryptedCode);
v=DecryptedCode&0x0000FFFF;
if(/*(Decrypted_Data.Hopping_Code&0x00FF0000)
==(Recived_Data.Serial_Number&0x000000FF)*/1) //
{
if(/*((v>KeyIDx.Sync_Counter1)&&((v-KeyIDx.Sync_Counter1)<=512)) //
||((v>KeyIDx.Sync_Counter2)&&((v-KeyIDx.Sync_Counter2)<=512))*/1)
{
//KeyIDx.Sync_Counter1 =v; //if validate ,update the Sync_Counter value
//KeyIDx.Sync_Counter2 =v;
WriteKeyID32(v,KeyIDx_Address.Sync_Counter1_Address);
WriteKeyID32(v,KeyIDx_Address.Sync_Counter2_Address);
//Sync_Counter_Store(Sync_Counter1,Sync_Counter2); //write code pace to store Sync_Counter1 and Sync_Counter2
if(ButtonAStatus&0x01) GPIOC->ODR |=0x0010;
else if(ButtonAStatus&0x02) GPIOC->ODR |=0x0020;
else if(ButtonAStatus&0x04) GPIOC->ODR |=0x0040;
else if(ButtonAStatus&0x08) GPIOC->ODR |=0x0080;
Delay_Xs(1);
GPIOC->ODR &=0x0000;
for(i=0;i<500;i++)
{
//Delay_XMs(1);
}
}else
{
//alarm
}
}else
{
//alarm
}
}
/*********************************************************************
Founction Name :KeyGen()
Founction :you can use this founction to generate the decrypt key
when the reciver is in learn mode. With this key generated ,
you can decrypt the encrypted partion of the recived code .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -