Monday 21 October 2013

Some exercises with the "Caesar cipher" and Arduino Pt.1



First instalment of the first cryptographic cipher - The Caesar Cipher.

I will be making a more in-depth explanation of how it all works, as well as adapt the code for a Serial.read ( ) for better usage as proof of concept for this specific project !

For now, here is the code !


//              "CAESAR CIPHER"
//   A simple "CAESAR CIPHER"ing project - http://dubworks.blogspot.co.uk/2013/10/some-exercises-with-caesar-cipher-with_22.html
//  Number_of_Characters = 96 also known as Keyspace
//  [ 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= 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[]=
{   
  '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];
    //    Serial.print(" P original- - ");
    //    Serial.println(retrieved);
    retrieved -=32; // Subract Ascii_offset
    //    Serial.print(" P - Offset = ");
    //    Serial.println(retrieved);
    uint16_t c =(retrieved + 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.print("c- ");
    //    Serial.println(c);
    //    Serial.print("C_array- ");
    //    Serial.println(ciphered[i]);
    //    Serial.print("Alphaarray- ");
    //    Serial.println(plaintext[i]);
    delay(50);
  }
  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);
  }
}




A slight different sketch here, easier to test other examples
http://forum.arduino.cc//index.php?topic=194628.msg1437373#msg1437373


Or a more readable version of the code here https://docs.google.com/document/d/1CbK-v1OaQIhbr0ao5EY8CrGZ71M9-t4cav4bK4JBwo0/edit?usp=sharing




-More insight into it !
http://dubworks.blogspot.co.uk/2013/10/some-exercises-with-caesar-cipher-with_22.html

http://dubworks.blogspot.co.uk/2013/10/some-exercises-with-caesar-cipher-pt-2.html

No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless