Cleaned up code and implemented encryption

This commit is contained in:
leca 2023-07-10 11:57:50 +03:00
parent a3e0c69631
commit 920eeb1f84
1 changed files with 28 additions and 10 deletions

View File

@ -18,13 +18,12 @@ const byte micPin = A0;
//byte unsigned address = 1; //byte unsigned address = 1;
const byte address[6] = "00001"; const byte address[6] = "00001";
unsigned short key = 0; byte key = 97;
byte unsigned screen = 0; byte unsigned screen = 0;
const String seed = "1305b5cfa1a59e9789de"; // "random" seed ;) const char seed[32] = "555bfda9ba33008ceba78321683ed8e"; // random seed, MUST BE 31 chars long
String password; char password[32];
bool mode = 0; // 0 = transmit, 1 = recieve, bool is 2 bytes less than byte :) bool mode = 0; // 0 = transmit, 1 = recieve
bool encryption = 1; // will the trafic be encrypted? bool encryption = 1; // will the trafic be encrypted?
byte value = 0;
bool isScreenChanged = true; // we must display first time bool isScreenChanged = true; // we must display first time
@ -43,11 +42,13 @@ byte decreaseButtonLast;
void switchMode(); void switchMode();
void printOutput(); void printOutput();
void updatePassword(); void updatePassword();
void encryptDecyptData();
//short micValue = 337; //short micValue = 337;
//const short micBaseline = 337;// 3.3v / 2 = 1.56v, 1.56/5*1024 = 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() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -63,7 +64,6 @@ void setup() {
radio.setChannel(0x00); radio.setChannel(0x00);
radio.setDataRate(RF24_250KBPS); radio.setDataRate(RF24_250KBPS);
switchMode(); //default is recieving switchMode(); //default is recieving
switchMode();
//Setting up a LCD //Setting up a LCD
lcd.init(); lcd.init();
lcd.backlight(); lcd.backlight();
@ -74,14 +74,16 @@ void setup() {
void loop() { void loop() {
switchMode(); switchMode();
if (!mode) {// transmit if (!mode) {// transmit
for (int i = 0; i < 32; i ++) { for (int i = 0; i < micValuesLength; i ++) {
micValues[i] = analogRead(micPin) << 2; micValues[i] = analogRead(micPin) << 2;
} }
encryptDecyptData();
radio.write(micValues, 32); radio.write(micValues, 32);
} else { // receive } else { // receive
if (radio.available()) { if (radio.available()) {
radio.read(micValues, 32); radio.read(micValues, 32);
for (int i = 0; i < 32; i++) { encryptDecyptData();
for (int i = 0; i < micValuesLength; i++) {
PORTD = micValues[i]; PORTD = micValues[i];
} }
} }
@ -171,5 +173,21 @@ void printOutput () {
} }
void updatePassword() { 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]);
}
} }