Go Down

Topic: Caeser cipher (Read 67 times) previous topic - next topic

xxxevil

i have searched everywhere looking for a Caesar cipher but i only found one or two for the Arduino so i created one using them as reference

I'm just listing it here for future arduino users with comments

i used a xbee pro for this coding and the coding is very compact

Code: [Select]

#include <SoftwareSerial.h>
SoftwareSerial zigBee(3,2);
char message;  int cases = 0;  int shift = 0; int choice = 0;  char cases2;
void setup()
{ // connecting to the serial monitour and zigbee
      zigBee.begin(9600);  Serial.begin(9600);
      Serial.println(" ");  Serial.println("  to transfer enter number 1");  Serial.println("  To receive enter number 2");
  // check if anything is writen into the serial monitour
  while (Serial.available()<=0){}
  if (Serial.available()>=0){    choice = Serial.read();}}
void loop(){ // the tranismitting part of the caesaer cipher
    if (choice==49)    {// the number 1 in ascii format // making sure something is entered into the serial
          Serial.println("  Please enter the amount of shifts between 1 and 25");
      while (Serial.available()<=0){} // if the serial monitour
      if (Serial.available()>=0){  shift = Serial.parseInt();
      if (shift <26){
      // makeing sure the shift amouint if less than 26 same as the aplpha bet
      // print to the serial monitour and the zigbee with a delay to make sure the user has enough time to enter the shift amount
          Serial.println("  shift amount accepted!");   Serial.print("the shift entered is: ");   Serial.println(shift); zigBee.print(shift);      delay(500);}
          // if the shift amount is more than 25 than they will be mmet with an error message and loop back to the begining
      else {  Serial.println("  the amount of shifts entered is invalid");   delay(500);   loop();   }}
      // if the shift amount is correct than they can start to enter their message
              Serial.println("  Enter Message");
      while (Serial.available()>=0){ if (Serial.available()>0){ cases = Serial.read();
      // the cases is red to make sure the letters entered is either upper case or lowercase using the ascii format 65 to upper case A
      if (cases>='A' && cases<='Z'  ){ // if ther cases are upper case then print them in both the serial monitour and the zigbee
             cases-=65;  message = (cases+shift)%26;    message +=65;  cases2=cases+65; Serial.print(message); zigBee.print(cases2); delay(1000);  message = 0;
            } // checking if the case is lower case and then printing them into the serial monitour abd zugbee
          else  if (cases>='a' && cases<='z'){  cases-=97;    message = (cases+shift)%26;    message +=97; cases2= cases +97;Serial.print(message); zigBee.print(cases2);  delay(1000); message = 0;  }
            // if the letters is a symbol or number then print blank or space
          else  if (cases == ' '  ){      message= cases;  Serial.print(message);   zigBee.print(" ");   delay(1000);   message = 0;}}}       }
      // the recieving part of the caeser cipher
      if (choice==50){ // the number 2 in ascii format      
        Serial.println("  conversion in progress");      
        // this part will check if anything was sent and check the shift amount and convert it into letters
      while (zigBee.available()>=0){ if (zigBee.available()){ shift = zigBee.parseInt();          
      // if the shift amount was less than
      if (shift<26){Serial.println("  conversion completed");
            Serial.println("  Receiving shift amount to decrypt");
            // is there something in the zigbee then read it and print it and if there isnt keep loopping
            while (zigBee.available()>=0)
            {  if (zigBee.available()) {cases2 = zigBee.read();Serial.print(cases2);}}}
          else {  loop();}}}}  
          else {  Serial.println("  error only 1 or 2 are allowed to choose between reciever and transmiter");      delay(1000);      setup();   }}  





Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy