Home / Expert Answers / Computer Science / in-the-context-of-the-developed-system-application-within-tinkercad-the-objective-is-to-enhance-com-pa362

(Solved): In the context of the developed system application within Tinkercad, the objective is to enhance com ...



In the context of the developed system application within Tinkercad, the objective is to enhance communication security. This involves implementing cryptographic measures to safeguard data transmission. Specifically, the task includes the utilization of the vigenère cipher method for securing messages transmitted from the Master Arduino to the Slave Arduinos within the established communication protocol. Screenshots are provided so follow the connections and enhance and fix the code given, you may remove the XOR Cipher if it helps:

Key Deliverables:

  1. Tinkercad Code: Provide the correct code for each Arduino showcasing the secure communication setup.
  2. vigenère cipher Method: Make sure to put both the encryption method and the decryption of the vigenère cipher method.
  3. Runtime Screenshots: Include pertinent runtime screenshots (Use the monitor to print) illustrating the following stages of the communication process:

a) Plain text before encryption at the sender's (Master) side.

b) Cipher text after encryption at the sender's (Master) side.

c) Decrypted plain text at the receiver's (Slave) side.



1. The code for the Master Arduino Uno R3 in TinkerCad is:
#include <Wire.h>
#include <Keypad.h> enum {ProtocolHeader, Command1, Argument1, Command2, Argument2}; const int ROW_NUM = 4; const int COLUMN_NUM = 3; char keys[ROW_NUM][COLUMN_NUM] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'*','0','#'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; byte pin_column[COLUMN_NUM] = {5, 4, 3}; Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); const String password = "3899"; String input_password; int attemptCount = 0; byte message[5]; #define NotUsed 'N' void buildMessage(byte Cmd1, byte Arg1, byte Cmd2, byte Arg2) { message[ProtocolHeader] = 'X'; message[Command1] = Cmd1; message[Argument1] = Arg1; message[Command2] = Cmd2; message[Argument2] = Arg2; } void sendMessage(int SlaveAddress) { String plainText = String(message, 5); String key = "ABCDEF"; String encryptedText = xorCipher(vigenereCipher(plainText, key), key); encryptedText.toCharArray(message, 5); Wire.beginTransmission(SlaveAddress); Wire.write(message, 5); Wire.endTransmission(); Serial.println("sending message"); } void setup() { Wire.begin(); Serial.begin(9600); input_password.reserve(5); // Enable internal pull-up resistors for the keypad for (int i = 0; i < ROW_NUM; i++) { pinMode(pin_rows[i], INPUT_PULLUP); } } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; } else if (key == '#') { if (password == input_password) { Serial.println("correct password entered"); buildMessage(99, 50, NotUsed, NotUsed); sendMessage(4); sendMessage(6); attemptCount = 0; } else { attemptCount++; if (attemptCount >= 3) { buildMessage(99, 70, NotUsed, NotUsed); sendMessage(4); sendMessage(6); attemptCount = 0; } else { buildMessage(99, 60, NotUsed, NotUsed); sendMessage(4); sendMessage(6); Serial.println("TRY AGAIN !"); } } input_password = ""; } else { input_password += key; } } } String vigenereCipher(String plainText, String key) { String cipherText = ""; int keyIndex = 0; for (int i = 0; i < plainText.length(); i++) { char plainChar = plainText.charAt(i); char keyChar = key.charAt(keyIndex); char cipherChar = (plainChar + keyChar) % 256; cipherText += cipherChar; keyIndex = (keyIndex + 1) % key.length(); } return cipherText; } String xorCipher(String text, String key) { String cipherText = ""; for (int i = 0; i < text.length(); i++) { char plainChar = text.charAt(i); char keyChar = key.charAt(i % key.length()); char cipherChar = plainChar ^ keyChar; cipherText += cipherChar; } return cipherText; }

2. The code for the Slave 4 Arduino Uno R3 In TinkerCad:
#include <Wire.h>
enum {ProtocolHeader, Command1, Argument1, Command2, Argument2}; enum {current, CorrectPass, InCorrectPass}; int z=0; byte message[5]; void receiveEvent(int howMany) { if (Wire.available() > 0) { message [ProtocolHeader]=Wire.read(); if (message [ProtocolHeader]=='X') { message [Command1]=Wire.read(); message [Argument1]=Wire.read(); message [Command2]=Wire.read(); message [Argument2]=Wire.read(); printMessage(); decodeAndExecute(); } } } String vigenereCipher(String plainText, String key) { String cipherText = ""; int keyIndex = 0; for (int i = 0; i < plainText.length(); i++) { char plainChar = plainText.charAt(i); char keyChar = key.charAt(keyIndex); char cipherChar = (plainChar + keyChar) % 256; cipherText += cipherChar; keyIndex = (keyIndex + 1) % key.length(); } return cipherText; } String xorCipher(String text, String key) { String cipherText = ""; for (int i = 0; i < text.length(); i++) { char plainChar = text.charAt(i); char keyChar = key.charAt(i % key.length()); char cipherChar = plainChar ^ keyChar; cipherText += cipherChar; } return cipherText; } void decodeAndExecute() { // Decrypt the message using Vigenere Cipher and XOR Cipher String encryptedText = String(message, 5); String key = "ABCDEF"; // Replace with the same key used for encryption String decryptedText = vigenereCipher(xorCipher(encryptedText, key), key); byte Command = decryptedText[Command1] - '0'; // Convert char to byte byte Pass = decryptedText[Argument1] - '0'; // Convert char to byte Serial.println(Pass); if (Pass==50) { Serial.println ("turn on the bulb"); digitalWrite(12,HIGH); //turn on bulb //delay(2000); //digitalWrite(12,LOW); } else if (Pass==60) { digitalWrite(10,HIGH);//red LED turned on digitalWrite(8,HIGH); //buzzer turned on delay(2000); digitalWrite(10,LOW); digitalWrite(8,LOW); delay(2000); } else if (Pass==70) { digitalWrite(8,HIGH); //buzzer turned on delay(1000); digitalWrite(8,LOW); delay(1000); } } void printMessage() { Serial.print("Protocol Header: "); Serial.println((char) message[ProtocolHeader]); Serial.print("Command 1: "); Serial.println(message[Command1]); Serial.print("Argument 1: "); Serial.println(message[Argument1]); Serial.print("Command 2: "); Serial.println(message[Command2]); Serial.print("Argument 2: "); Serial.println(message[Argument2]); Serial.println("---------------------------------"); } void setup() { pinMode(12,OUTPUT); pinMode(10,OUTPUT); pinMode(8,OUTPUT); pinMode(2,INPUT); Wire.begin(6); Wire.onReceive(receiveEvent); Serial.begin(9600); } void loop() { }

The code for the Slave 6 Arduino Uno R3 In TinkerCad:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
enum {ProtocolHeader, Command1, Argument1, Command2, Argument2}; Servo myservo; byte message[5]; LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void receiveEvent(int howMany) { Serial.println("received"); if (Wire.available() > 0) { message [ProtocolHeader]=Wire.read(); if (message [ProtocolHeader]=='X') { message [Command1]=Wire.read(); message [Argument1]=Wire.read(); message [Command2]=Wire.read(); message [Argument2]=Wire.read(); printMessage(); decodeAndExecute(); } } } String vigenereCipher(String plainText, String key) { String cipherText = ""; int keyIndex = 0; for (int i = 0; i < plainText.length(); i++) { char plainChar = plainText.charAt(i); char keyChar = key.charAt(keyIndex); char cipherChar = (plainChar + keyChar) % 256; cipherText += cipherChar; keyIndex = (keyIndex + 1) % key.length(); } return cipherText; } String xorCipher(String text, String key) { String cipherText = ""; for (int i = 0; i < text.length(); i++) { char plainChar = text.charAt(i); char keyChar = key.charAt(i % key.length()); char cipherChar = plainChar ^ keyChar; cipherText += cipherChar; } return cipherText; } void decodeAndExecute() { // Decrypt the message using Vigenere Cipher and XOR Cipher String encryptedText = String(message, 5); String key = "ABCDEF"; // Replace with the same key used for encryption String decryptedText = vigenereCipher(xorCipher(encryptedText, key), key); byte Pass = decryptedText[Argument1] - '0'; // Convert char to byte if(Pass==50)//password entered in Master is correct { lcd.clear(); digitalWrite(9,LOW); digitalWrite(8,HIGH); myservo.write(90); lcd.setCursor(0, 0); lcd.print("WELCOME"); lcd.setCursor(0, 1); lcd.print("HOME"); myservo.write(90); delay(5000); } else if(Pass==60) //if any other password is entered { lcd.clear(); digitalWrite(9,HIGH); digitalWrite(8,LOW); lcd.setCursor(0, 0); lcd.print("INVALID"); lcd.setCursor(0, 1); lcd.print("CREDENTIALS"); delay(500); myservo.write (0); } else if (Pass==70) //3 attempts passed { lcd.clear(); digitalWrite(9,HIGH); digitalWrite(8,LOW); lcd.setCursor(0, 0); lcd.print("LOCKED"); myservo.write (0); delay(5000); } } void printMessage() { Serial.print("Protocol Header: "); Serial.println((char) message[ProtocolHeader]); Serial.print("Command 1: "); Serial.println(message[Command1]); Serial.print("Argument 1: "); Serial.println(message[Argument1]); Serial.print("Command 2: "); Serial.println(message[Command2]); Serial.print("Argument 2: "); Serial.println(message[Argument2]); Serial.println("---------------------------------"); } void setup() { pinMode(10,OUTPUT); pinMode(9,OUTPUT); pinMode(8,OUTPUT); pinMode(7,OUTPUT); Wire.begin(4); Wire.onReceive(receiveEvent); lcd.begin(16, 2); myservo.attach(9); Serial.begin(9600); } void loop() { //digitalWrite(9,HIGH); //digitalWrite(8,HIGH); //myservo.write (0); lcd.clear(); lcd.setCursor(0, 0); lcd.print("ENTER PASSWORD"); }

The code for the Attacker Arduino Uno R3 In TinkerCad:
#include <Wire.h>
void transmission(int f, int d)
{ Wire.beginTransmission(d); //d is device number Wire.write(f); //f is the value Wire.endTransmission(); } void setup() { Wire.begin(); Serial.begin(9600); } int i=40; void loop() { transmission(i,4); i++; delay(1000); }



We have an Answer from Expert

View Expert Answer

Expert Answer



We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe