CCCPaste

"Arduino Encryption" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Arduino Encryption"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-11 16:23:15

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* When I go to insert a message from the Arduino */
	/* serial monitor, it will encrypt it according to */
	/* Caesar's cipher with key = 5 and send it to a */
	/* second Arduino */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Later, when the second arduino has received the */
	/* encrypted message from the first arduino, it will */
	/* decrypt the message and print it on its serial */
	/* monitor */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void encryptMessage(String& message);
void decryptMessage(String& message);

/***** DEFINITION OF I2C PINS *****/
const uint8_t Monitor_SSD1306OledDisplay_I2C_PIN_SDA = A4;
const uint8_t Monitor_SSD1306OledDisplay_I2C_PIN_SCL = A5;
const uint8_t Monitor_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Adafruit_SSD1306 display(Monitor_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;

/****** GLOBAL VARIABLES *****/
const int caesarKey = 5;

void setup(void)
{
    // Initialize I2C communication
    Wire.begin();

    // Initialize SSD1306 display
    display.begin(SSD1306_SWITCHCAPVCC);
    u8g2_for_adafruit_gfx.begin(display);
}

void loop(void)
{
    // Check for input from serial monitor
    if (Serial.available())
    {
        String message = Serial.readString();
        encryptMessage(message);
        Serial.println("Encrypted message: " + message);
    }

    // Check for received encrypted message from the first Arduino
    // and decrypt it
    // Once decrypted, print the message on the serial monitor
    // Example: if (receivedEncryptedMessage) { decryptMessage(receivedMessage); Serial.println("Decrypted message: " + receivedMessage); }
}

void encryptMessage(String& message)
{
    for (int i = 0; i < message.length(); i++)
    {
        if (isAlpha(message[i]))
        {
            char baseChar = isUpperCase(message[i]) ? 'A' : 'a';
            message[i] = ((message[i] - baseChar + caesarKey) % 26) + baseChar;
        }
    }
}

void decryptMessage(String& message)
{
    for (int i = 0; i < message.length(); i++)
    {
        if (isAlpha(message[i]))
        {
            char baseChar = isUpperCase(message[i]) ? 'A' : 'a';
            message[i] = ((message[i] - baseChar - caesarKey + 26) % 26) + baseChar;
        }
    }
}