Advertisement
pleasedontcode

"Arduino Encryption" rev_01

Dec 11th, 2023
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Arduino Encryption"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-11 16:23:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* When I go to insert a message from the Arduino */
  21.     /* serial monitor, it will encrypt it according to */
  22.     /* Caesar's cipher with key = 5 and send it to a */
  23.     /* second Arduino */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* Later, when the second arduino has received the */
  26.     /* encrypted message from the first arduino, it will */
  27.     /* decrypt the message and print it on its serial */
  28.     /* monitor */
  29. /****** END SYSTEM REQUIREMENTS *****/
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <Arduino.h>
  33. #include <Wire.h>
  34. #include <Adafruit_GFX.h>
  35. #include <Adafruit_SSD1306.h>
  36. #include <U8g2_for_Adafruit_GFX.h>
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41. void encryptMessage(String& message);
  42. void decryptMessage(String& message);
  43.  
  44. /***** DEFINITION OF I2C PINS *****/
  45. const uint8_t Monitor_SSD1306OledDisplay_I2C_PIN_SDA = A4;
  46. const uint8_t Monitor_SSD1306OledDisplay_I2C_PIN_SCL = A5;
  47. const uint8_t Monitor_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C;
  48.  
  49. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  50. Adafruit_SSD1306 display(Monitor_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  51. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  52.  
  53. /****** GLOBAL VARIABLES *****/
  54. const int caesarKey = 5;
  55.  
  56. void setup(void)
  57. {
  58.     // Initialize I2C communication
  59.     Wire.begin();
  60.  
  61.     // Initialize SSD1306 display
  62.     display.begin(SSD1306_SWITCHCAPVCC);
  63.     u8g2_for_adafruit_gfx.begin(display);
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     // Check for input from serial monitor
  69.     if (Serial.available())
  70.     {
  71.         String message = Serial.readString();
  72.         encryptMessage(message);
  73.         Serial.println("Encrypted message: " + message);
  74.     }
  75.  
  76.     // Check for received encrypted message from the first Arduino
  77.     // and decrypt it
  78.     // Once decrypted, print the message on the serial monitor
  79.     // Example: if (receivedEncryptedMessage) { decryptMessage(receivedMessage); Serial.println("Decrypted message: " + receivedMessage); }
  80. }
  81.  
  82. void encryptMessage(String& message)
  83. {
  84.     for (int i = 0; i < message.length(); i++)
  85.     {
  86.         if (isAlpha(message[i]))
  87.         {
  88.             char baseChar = isUpperCase(message[i]) ? 'A' : 'a';
  89.             message[i] = ((message[i] - baseChar + caesarKey) % 26) + baseChar;
  90.         }
  91.     }
  92. }
  93.  
  94. void decryptMessage(String& message)
  95. {
  96.     for (int i = 0; i < message.length(); i++)
  97.     {
  98.         if (isAlpha(message[i]))
  99.         {
  100.             char baseChar = isUpperCase(message[i]) ? 'A' : 'a';
  101.             message[i] = ((message[i] - baseChar - caesarKey + 26) % 26) + baseChar;
  102.         }
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement