Copy Code code as follows:
Random number encryption Algorithm a^b = C, A^c =b, b^c = A xor or encryption
Etual 2011-3-14
#include <stdio.h>
7byte Data and 1byte key
unsigned char code_buf[8] = {0x12,0x13,0x14,0x15,0x21,0x22,0x23,0x00};
void Print_buf (void)
{
int i;
for (i=0;i<8;i++)
{
printf ("%x,", Code_buf[i]);
}
printf ("\ n");
}
int main (void)
{
unsigned char key,new_key;
unsigned char Rand_digi;
int i;
printf ("Original buf is!\n");
Print_buf ();
Key is a private key that is owned by each of the two communications (required for encryption and decryption)
key = 0x55;
Rand_digi is a random number
Rand_digi = 0xe3; Can be a time stamp
Using private key to encrypt random number to get ciphertext as new key
New_key = key ^ Rand_digi;
Encrypt and encrypt data with this new key
for (i=0;i<7;i++)
{
Code_buf[i] ^= New_key;
}
This key is sent along with the data.
CODE_BUF[7] = New_key;
printf ("Encrypted buf is!\n");
Print_buf ();
Decryption process
Assuming the receiving end receives the encrypted 8-byte data correctly
printf ("Now decode:\n");
Because the last byte is key, use this to decrypt the previous data
for (i=0;i<7;i++)
{
Code_buf[i] ^= code_buf[7];
}
The last one is also encrypted, using the private key to restore can get random number
CODE_BUF[7] ^= key;
Print_buf ();
return 0;
}