Transmition Encrypt and Decrypt Code in Arduino - Tann Thona

Telecommunication and Electronic Engineering

Post

Post Top Ad

Wednesday, January 16, 2019

Transmition Encrypt and Decrypt Code in Arduino

Send Message Encrypt and Decrypt in Arduino

1. Objective

The objective of this project is talk about,  use serial communication to send message with unreadable by use encrypt method that make someones who try hack our message. Receiver can decrypt message for readable because transmitter and receiver have the same reference.

2. Requirement

  • Arduino UNO (x2)
  • USB Cable
  • Button
  • Electronic Wire

3. Building

The first build circuit like below on Figure 1.

Figure 1: Serial Communication Encrypt and Decrypt



4. Coding

Transmitter Code:

      
char Ref[94] = {'0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                '`','~','!','@','#','$','%','^','&','*','(',')','-','_','+','=','[',']','{','}','|',';',':','"','<','>',
                '/','?','.',',','\'','\\'};

int Encrypt[94] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
                   36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,
                   69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93};

int button = 13;
String msg = "RUPP_MY002_L1_ON";
String a = "3";

void setup() {
  Serial.begin(9600);
  pinMode(button,INPUT_PULLUP);
}

void encryption(String str) {

  int msgLength = 0;
  int RefLength = 0;
  String dataEncrypt[20];

  //Serial.println(str);
  RefLength = sizeof(Ref);
  msgLength = str.length();

  // Encrypt
  for(int i=0;i<msgLength;i++) {
    for(int j=0;j<RefLength;j++) {
      if(msg[i]==Ref[j]) {
        dataEncrypt[i] = String(Encrypt[j]);
      }
    }
    Serial.print(dataEncrypt[i]);
    delay(30);
  }
}

void loop() {
  if(digitalRead(button)==0) {
    encryption(msg);
  }
}
 


Receiver Code:

#define Ref '_'
char Index[94] = {'0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                '`','~','!','@','#','$','%','^','&','*','(',')','-','_','+','=','[',']','{','}','|',';',':','"','<','>',
                '/','?','.',',','\'','\\'};

int Encrypt[94] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,
                   36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,
                   69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93};

String msg[20];
String data[20];
String dataDecrypt;

int button = 13;
int i = 0;

void setup() {
  pinMode(button,INPUT_PULLUP);
  Serial.begin(9600);
  Serial.setTimeout(3);
}

void AnalyseMsg(String msg) {
  
  int msgLength = 0;      // Total of Message length
  int cnt = 0;            // Count Reference number
  int refLocate[10] = {0};  // Location of the Reference in the message
  int x = 0;              // Variable for helping arrange the msg
  int y = 0;              // Variable for helping arrange the msg
  msgLength = msg.length() - 1;

  for(int i=0;i<msgLength+1;i++) {
    if(Ref == msg[i]) {
      cnt++;
      refLocate[cnt] = i;
    }
  }
  //Serial.println(cnt); 
  refLocate[cnt+1] = msgLength;
  for(int j=0;j<cnt+1;j++) {
    if(refLocate[j+1] == msgLength) {
      y = 1;
    }
    for(int i=refLocate[j]+x;i<refLocate[j+1]+y;i++) {
      data[j] += msg[i];
    }
    Serial.println(data[j]);
    x = 1;
  }
  // Clear data
  for(int i=0;i<cnt+2;i++) {
    data[i] = "";
  }
}

void decryption() {
  for(int j=0;j<i;j++) {
    dataDecrypt += Index[msg[j].toInt()];
  }
  //Serial.println(dataDecrypt);
  AnalyseMsg(dataDecrypt);
  dataDecrypt = "";
  i = 0;
}

void Readmsg() {
  if(Serial.available()) {
    msg[i] = Serial.readString();
    //Serial.println(msg[i]);   
    i++;
  }
}

void loop() {
  Readmsg();
  if(digitalRead(button) == 0) {
    decryption();
    delay(200);
  }
}
 



5. Result

In the result when we push transmitter's button, the transmitter send message that we set in code to receiver with encrypt message. if we want receive message from transmitter in decrypt with readable just push receiver's button. In this case If we push receiver's button before push transmitter's button transmit "that mean transmitter don't send message to receiver", the text receive will errors.

Figure 2: Result Sending Message with Encrypt and Decrypt in Proteus Simulation.









Royal University of Phnom Penh
Faculty of Engineering
Dep. Telecommunication and Electronic Engineering

Group Member:
1. Tann Thona
2. Thach Soveasna
3. Chhoy Noreath
4. Neth Channa
5. Mok Vira

Instructor: Prof. Chann Tola
Date: 12 May 2017

No comments:

Post a Comment

Post Top Ad