From 920eeb1f848c45e3d01e98c8736087eaffd54f00 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 10 Jul 2023 11:57:50 +0300 Subject: [PATCH] Cleaned up code and implemented encryption --- code/code.ino | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/code/code.ino b/code/code.ino index 411d22e..7b38be5 100644 --- a/code/code.ino +++ b/code/code.ino @@ -18,13 +18,12 @@ const byte micPin = A0; //byte unsigned address = 1; const byte address[6] = "00001"; -unsigned short key = 0; +byte key = 97; byte unsigned screen = 0; -const String seed = "1305b5cfa1a59e9789de"; // "random" seed ;) -String password; -bool mode = 0; // 0 = transmit, 1 = recieve, bool is 2 bytes less than byte :) +const char seed[32] = "555bfda9ba33008ceba78321683ed8e"; // random seed, MUST BE 31 chars long +char password[32]; +bool mode = 0; // 0 = transmit, 1 = recieve bool encryption = 1; // will the trafic be encrypted? -byte value = 0; bool isScreenChanged = true; // we must display first time @@ -43,11 +42,13 @@ byte decreaseButtonLast; void switchMode(); void printOutput(); void updatePassword(); +void encryptDecyptData(); //short micValue = 337; //const short micBaseline = 337;// 3.3v / 2 = 1.56v, 1.56/5*1024 = 337. -short micValues[32]; +const byte micValuesLength = 16; +short micValues[micValuesLength]; void setup() { Serial.begin(9600); @@ -63,7 +64,6 @@ void setup() { radio.setChannel(0x00); radio.setDataRate(RF24_250KBPS); switchMode(); //default is recieving - switchMode(); //Setting up a LCD lcd.init(); lcd.backlight(); @@ -74,14 +74,16 @@ void setup() { void loop() { switchMode(); if (!mode) {// transmit - for (int i = 0; i < 32; i ++) { + for (int i = 0; i < micValuesLength; i ++) { micValues[i] = analogRead(micPin) << 2; } + encryptDecyptData(); radio.write(micValues, 32); } else { // receive if (radio.available()) { radio.read(micValues, 32); - for (int i = 0; i < 32; i++) { + encryptDecyptData(); + for (int i = 0; i < micValuesLength; i++) { PORTD = micValues[i]; } } @@ -171,5 +173,21 @@ void printOutput () { } void updatePassword() { - password = seed + key; + for (int i = 0; i < 31; i ++) { + password[i] = seed[i]; + } + password[31] = (char)key; +} + +void encryptDecyptData () { + for (int i = 0; i < micValuesLength; i ++) { + char localKey[2]; + localKey[0] = password[2 * i]; + localKey[1] = password[2 * i + 1]; + Serial.print("Before:"); + Serial.print(micValues[i]); + micValues[i] = micValues[i] ^ (short)localKey; + Serial.print(",After:"); + Serial.println(micValues[i]); + } }