Go Down

Topic: "Caesar cipher" with Arduino Pt.1 (Read 3172 times) previous topic - next topic

iyahdub

Oct 21, 2013, 07:53 pm Last Edit: Oct 21, 2013, 09:07 pm by iyahdub Reason: 1
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])

http://dubworks.blogspot.co.uk/2013/10/some-exercises-with-caesar-cipher-with.html
More soon to come !
10 LET Loop=Infinite
20 GO TO 10

iyahdub

#1
Oct 22, 2013, 12:33 am Last Edit: Oct 22, 2013, 02:00 am by iyahdub Reason: 1
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 !
Code: [Select]
//              "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);
  }
}




10 LET Loop=Infinite
20 GO TO 10

Go Up