Here is my final code . It sends and receives data but not the data what I expect.
I sent
unsigned long dataToEncrypt [2] = {0x01234567, 0x89abcdef};
I must receive
{0x126c6b92 , 0xc0653a3e}
But I did not get what I want. I do not Know where is the problem at the program logic or somewhere else.
TX Code:
#include <LiquidCrystal.h>
#include <VirtualWire.h>
#include <SoftwareSerial.h>
// Decleration Part
#define DATA_LENGTH 64 // bits number of data is .
// data to encrypt
unsigned long dataToEncrypt [2] = {0x01234567, 0x89abcdef};
// The Key of encryption and decryption
unsigned long key [4] = {0x00112233,0x44556677,0x8899aabb,0xccddeeff};
// The encrypted data that is ready to send.
unsigned char dataToSend[8];
// pointer to dataToEncrypt
//unsigned long* ptrToData;
//Data pointer initialization
//ptrToData = (unsigned long*) dataToEncrypt;
// pointer to Key
//unsigned long* ptrToKey;
//Key pointer initialization
//ptrToData =(unsigned long*) key;
void setup() {
Serial.begin (9600);
// Initialise the IO and ISR
vw_set_tx_pin(8); // RF Transmitter pin data will connect to pin #8.
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
}
// TEA Encryption Function code:
void encrypt (unsigned long* v, unsigned long* k) {
unsigned long v0=v[0], v1=v[1], sum=0;int i; /* set up */
unsigned long delta=0x9e3779b9; /* a key schedule constant */
unsigned long key0=key[0], key1=key[1], key2=key[2], key3=key[3]; /* cache key */
for (i=0; i < 32; i++) { /* basic cycle start */
sum += delta;
v0 += ((v1<<4) + key0 ^ (v1 + sum) ^ ((v1>>5) + key1));
v1 += ((v0<<4) + key2 ^ (v0 + sum) ^ ((v0>>5) + key3));
} /* end cycle */
v[0]=v0; v[1]=v1;
}// end of the encryption function
void loop()
{
// char ascii[32];
//sprintf(ascii, "original: 0x%08X 0x%08X \n",(int)dataToEncrypt[0],(int)dataToEncrypt[1]); // ?????????
// print the plain text to the serial port
//Serial.print(ascii);
// Calling the encryption function
encrypt(dataToEncrypt,key);
// sprintf(ascii,"encrypted:0x%08X 0x%08X \n",(int)dataToEncrypt[0],(int)dataToEncrypt[1]); // ??????????
// print the cypher text to the serial port
//Serial.print((int)dataToEncrypt ,HEX);
//delay(10000);
// To chuncks the cypher text array to char type to send it through RF module using virtualWire library
// Convert 2x32 array to 8x8 array.
unsigned const long hexToByte[4]={ 0xff000000,0x00ff0000,0x0000ff00,0x000000ff};
int j = 0 ; // To count dataToSend elements
while ( j < 8 ){
for ( char i = 0 ; i < 2; i++){
for ( char c = 0 ; c < 4; c++){
dataToSend[j] = dataToEncrypt [i] & hexToByte[c];
j++;
} // end of the inner for loop.
}// end of the outer loop.
}// enf of while loop.
// start data transmitting through the RF Module.
vw_send((uint8_t *)dataToSend, 8);
vw_wait_tx(); // Wait for message to finish
delay(2000); // delay befor start the the void loop() again.
}
RX code
#include <LiquidCrystal.h>
#include <VirtualWire.h>
#include <SoftwareSerial.h>
// Decleration Part
#define DATA_LENGTH 64 // bits number of data is .
// data to decrypt
unsigned long dataToDecrypt [2];
// The Key of encryption and decryption
unsigned long key [4] = {0x00112233,0x44556677,0x8899aabb,0xccddeeff};
// The encrypted data that is ready to receive.
unsigned char dataToReceive[8];
// pointer to dataToDecrypt
//unsigned long* ptrToData;
//Data pointer initialization
//ptrToData =(unsigned long*) dataToDecrypt;
// pointer to Key
//unsigned long* ptrToKey;
//Key pointer initialization
//ptrToData =(unsigned long*) key;
unsigned long recivedData[8];
void setup() {
Serial.begin (9600); // boud per second for the serial port
// Initialise the IO and ISR
vw_set_rx_pin(9); // // We will be receiving on pin 9
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
// TEA Decryption Function code:
void decrypt (unsigned long* v, unsigned long* key) {
unsigned long v0=v[0], v1=v[1], sum=0xC6EF3720; int i ; /* set up */
unsigned long delta=0x9e3779b9; /* a key schedule constant */
unsigned long key0=key[0], key1=key[1], key2=key[2], key3=key[3]; /* cache key */
for (i=0; i<32; i++) { /* basic cycle start */
v1 -= ((v0<<4) + key2) ^ (v0 + sum) ^ ((v0>>5) + key3);
v0 -= ((v1<<4) + key0) ^ (v1 + sum) ^ ((v1>>5) + key1);
sum -= delta;
} // end of the decryption function
v[0]=v0; v[1]=v1;
}
void loop(){
unsigned const long byteToHEX=0x000000ff;
char ascii[32];
// "buf" is an arry to hold the rceived chunks ( Byte size )
uint8_t buf[VW_MAX_MESSAGE_LEN];
// "buflen" is byte size variable holds the max size of the message upon transmiting.
// Then during return "buflen" will hold the actual size of the received message.
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// check to see if anything has been received
if (vw_get_message(buf, &buflen)){
Serial.print("RECEIVING PROCESS STARTS ! \n");
Serial.print("Got: ");
// Print the received chunks to computer.
for (int i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(' ');
}
Serial.println();
}
// Group the received chunks of byte data type into dataToDecrypt array of unsigned long data type.
// copy the data from "buf" array to "recivedData" array
for( char m = 0 ; m < 8 ; m++){
recivedData[m] = (((unsigned long) buf[m]) &( byteToHEX));
} // end of copy loop
// Note the RF module send the LSB first.
for ( char k = 0 ; k < 8 ; k ++){
dataToDecrypt[0]= (recivedData[k]) | ((recivedData[k+1]) << 1) | ((recivedData[k+2]) << 2) | ((recivedData[k+3]) << 3) ;
dataToDecrypt[1]= (recivedData[k+4]) | ((recivedData[k+5]) << 1) | ((recivedData[k+6]) << 2) | ((recivedData[k+7]) << 3) ;
}
// Calling the encryption function
decrypt(dataToDecrypt,key);
sprintf(ascii,"decrypted:0x%08X 0x%08X \n",(int)dataToDecrypt[0],(int)dataToDecrypt[1]); // ??????????
//print the cypher text to the serial port
for (int i = 0 ; i<2 ;i++){
Serial.print(dataToDecrypt[i] , HEX);
Serial.print(" - \n");}
delay(1000);
}