Hello:
I am trying to implement RC4 encryption in MCU [i.e. Arduino UNO]. I want to set up 2 Xbee [radio] +2 Arduino for a small network where 1 Xbee+1 Arduino will work as transmitter [encrypts the data] and another set of Xbee +Arduino will receive the encrypted data and decrypt it. But before doing that I want to go step by step. So, at first I tried a code of RC4 in arduino which is connected with my PC. I checked it in arduino terminal and it worked ok.
Then I wanted to send the exact data which I am seeing in PC via a Xbee transmitter to another Xbee+Arduino [receiver]. The receiver is connected with my PC this time. But here I found a problem that the receiver only can capture one line at a time and shows that particular line only if I disable other Serial.print lines [but even if I enable only decryption it does not show the decrypted value in receiver]. It never shows anything at all if I try to show all the 4-5 lines [enabling all the Serial.print lines]. I think the problem is for error in my receiver code as it only takes serial data and again it is probably reading data before knowing about its size or it's existence. But I don't know how to solve it. I have been suggested to use Putty instead of the arduino terminal but still it does not show anything as a receiving end.
My query now is to know what modification I should make in my receiver code so that receiver can show all the output (4-5 lines) in my PC. My transmitter and receiver code is as below:
Transmitter:
#include <string.h>
unsigned char S[256];
char has[512];
#define S_SWAP(a,b) do { int t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
void rc4(char *key, char *data){
int i,j;
Serial.print("Source : ");
Serial.println(data);
Serial.print("Key : ");
Serial.println(key);
for (i=0;i<256;i++){
S[i] = i;
//printf("%x",S[i]);
}
//printf("\n");
j = 0;
for (i=0;i<256;i++){
j = (j+S[i]+key[i%strlen(key)]) %256;
S_SWAP(S[i],S[j]);
//printf("%x",S[i]);
}
//printf("\n");
i = j = 0;
for (int k=0;k<strlen(data);k++){
i = (i+1) %256;
j = (j+S[i]) %256;
S_SWAP(S[i],S[j]);
has[k] = data[k]^S[(S[i]+S[j]) %256];
}
has[strlen(data)+1] = '\0';
//printf("\n");
}
void setup() {
Serial.begin(9600);
char key[] = "Hello";
char sdata[] = "this is data source!";
rc4(key,sdata);
Serial.print("Encrypted : ");
Serial.println(has);
rc4(key,has);
Serial.print("Decrypted : ");
Serial.println(has);
}
void loop(){
}
Receiver:
void setup() {
Serial.begin(9600);
}
void loop(){
if(Serial.available()>0){
Serial.write(Serial.read());
}
}
Thank you for your time and patience.