//Random number encryption algorithm & NBSP; A to the B = C, A to the C =B, B to the C = A xor 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, which is held separately between two communications (both encryption and decryption are required).
key = 0x55;
//Rand_digi is a random number
rand_digi = 0xe3; //It could be a timestamp
//Encrypt the random number with the private key to get the ciphertext used as the new key
new_key = key ^ rand_digi;
//This new key is used to encrypt the data
for (i=0;i<7;i++)
{
code_buf[i] ^= new_key;
}
//The key is sent along with the data
code_buf[7] = new_key;
printf("encrypted buf is!n");
print_buf();
//The decryption process
//Suppose the receiver receives the encrypted 8-byte data correctly
printf("now decode:n");
//Since the last byte is the key, use this to decrypt the previous data
for (i=0;i<7;i++)
{
code_buf[i] ^= code_buf[7];
}
//The last one is itself encrypted, and a restore with the private key yields a random number
code_buf[7] ^= key;
print_buf();
return 0;
}