# "Caesar cipher" with Arduino Pt.1

**URL:** <https://forum.arduino.cc/t/caesar-cipher-with-arduino-pt-1/189683>\
**Category:** Showcase\
**Created:** [October 21, 2013, 5:53pm UTC](https://forum.arduino.cc/t/caesar-cipher-with-arduino-pt-1/189683 "2013-10-21T17:53:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iyahdub](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@iyahdub](https://forum.arduino.cc/u/iyahdub)\
**Post date:** [October 21, 2013, 5:53pm UTC](https://forum.arduino.cc/t/caesar-cipher-with-arduino-pt-1/189683/1 "2013-10-21T17:53:50Z")

</div>

I will be elaborating more on the how's and why's... But for now here is a simple proof of concept with Arduino .  
Next time will be integrated with Serial.read , but for now, this will do !  
Compatible with all the printable characters of the ascii table ( starting from SPACE [ascii 32] to ~[ascii 126])

> **[Some exercises with the "Caesar cipher" and Arduino Pt.1](http://dubworks.blogspot.com/2013/10/some-exercises-with-caesar-cipher-with.html)**
>
> Electronics; PIC's, Pinguino, AVR ;Arduino; Dub sirens/sound effects processors; Pro Audio; Robotics

More soon to come !

---

<div class="post-metadata">

**Author:** ![iyahdub](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@iyahdub](https://forum.arduino.cc/u/iyahdub)\
**Post date:** [October 21, 2013, 10:33pm UTC](https://forum.arduino.cc/t/caesar-cipher-with-arduino-pt-1/189683/2 "2013-10-21T22:33:05Z")

</div>

A more user friendly version of the code.  
As this subject includes maths as well, id guess this would be perfect for a subject in younger users workshops !

```cpp
//              "CAESAR CIPHER"
//  A simple "CAESAR CIPHER"ing project - http://dubworks.blogspot.co.uk/
//  Number_of_Characters = 96 
//  [ from "ascii'" character number 32-decimal; 040-OCT; 20-HEX; 00100000-BIN ==> "SPACE"
//  to "ascii'" character number 126 decimal;  176-OCT;  7E-HEX;  01111110-BIN; ==> "~" Equivalency sign - tilde
//
//  Caesar cipher ==> C=(P+s) Mod (Number_of_Characters)
//  P = numerical equivalent of character plaintext
//  C = Numerical equivalent of ciphertext character
//  s = number of shifts/adds
//  Ke== Enciphering Key
//  Kd== Deciphering key
//  Ke= s = 3 ==> (P+s) mod (Number_of_Characters)
//  Kd = -s = -3 ==> (C  -s)mod (Number_of_Characters)
//
//
// 
//  For more on Caesar cipher - http://en.wikipedia.org/wiki/Caesar_cipher
// 
#define Nchars 96  //  starting at 32 up to 126 in the ascii table 
#define n_shift 3  //  shift desired
//
char plaintext[]=
{ 
  "Arduino to cipher this#"};
//  'A','r','d','u','i','n','o',' ', 't','o',' ','c','i','p','h','e','r',' ','t','h','i','s', '#'};
//
char ciphered[sizeof(plaintext)];
//

boolean stringComplete = true;  // whether the string is complete

void setup() {
  // Insert your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  uint8_t i;
  //  Serial.println(sizeof(plaintext)); 
  //  Insert your main code here, to run repeatedly:  

  while(Serial)
  {
    if(stringComplete==true){
      Serial.println ("Arduino Caesar Cipher v1");
      Serial.println ("");
      cphr();
      //
      //
      Serial.println(" ");
      //
    }
  }
}

void cphr()
{
  uint8_t sizeofarray = sizeof(plaintext);
  uint8_t i;
  //
  for(i=0;i<sizeofarray;i++)
  {
    uint16_t retrieved;
    retrieved=plaintext[i];
    if(retrieved == 0){
      ciphered[i]=0;
      Serial.print("i- ");
      Serial.println(i);
      Serial.println("null CHARACTER ");
      // The use of goto's is highly frowned upon...
      // ...but can be quite handy as well ! 
      goto bailout; 
    }
    Serial.print("P original- - ");
    Serial.println(retrieved);
    //retrieved -=32; // Subract Ascii_offset
    Serial.print("P - Offset = ");
    Serial.println(retrieved-32);
    uint16_t c =((retrieved-32) + n_shift)% Nchars; // check blog post for more info on this   
    Serial.print("C original - ");
    Serial.println(c);
    c +=32; // add the Ascii_offset
    Serial.print("C + Offset = ");
    Serial.println(c);
    ciphered[i]=c;
    //
    Serial.println(" ");
    Serial.print("i- ");
    Serial.println(i);
    Serial.println(c);
    Serial.print("C_array- ");
    Serial.println(ciphered[i]);
    Serial.print("Alphaarray- ");
    Serial.println(plaintext[i]);
    delay(50);
  }
bailout://  :)
  displayResults();
  stringComplete = false;
}

void displayResults(){
  //
  uint8_t i;
  uint8_t sizeofarray = sizeof(plaintext);
  Serial.print("plaintext to encipher- ");
  for(i=0;i<sizeofarray;i++)
  {
    Serial.print(plaintext[i]);
    delay(10);
  }
  Serial.println(" ");
  Serial.print("Ciphered_array- ");
  for(i=0;i<sizeofarray;i++)
  {
    Serial.print(ciphered[i]);
    delay(50);
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:33pm UTC](https://forum.arduino.cc/t/caesar-cipher-with-arduino-pt-1/189683/3 "2021-05-06T12:33:25Z")

</div>

Closed on May 6, 2021, 12:33pm UTC


