//Created by Barbu Vulc!
//XOR key:
const byte key = 0x48;
//Text from keyboard...
char text[100];
void setup() {
//Initialization...
Serial.begin(115200);
Serial.println("Enter a text:");
//For emptying buffer.
memset(text, 0, sizeof(text));
pinMode(LED_BUILTIN, OUTPUT);
}
//Encrypt/decrypt function:
void encryptDecrypt(char* input, byte key) {
for (int i = 0; i < strlen(input); i++) {
//To encrypt/decrypt...
input[i] = input[i] ^ key;
}
}
void loop() {
//Write a text in Serial Monitor:
if (Serial.available() > 0) {
char receivedChar = Serial.read();
//Entered original text shown here:
if (receivedChar == '\n') {
Serial.print("The original text: ");
Serial.println(text);
//XOR encryption:
encryptDecrypt(text, key);
Serial.print("Encrypted text: ");
Serial.println(text);
digitalWrite(LED_BUILTIN, HIGH); delay(50);
digitalWrite(LED_BUILTIN, LOW); delay(50);
//Reset the buffer in order to enter a new text next time!
memset(text, 0, sizeof(text));
Serial.println("Introduce a new text and press Enter:");
} else {
if (strlen(text) < sizeof(text) - 1) {
text[strlen(text)] = receivedChar;
}
}
}
}