Showing posts with label RSA. Show all posts
Showing posts with label RSA. Show all posts

Wednesday, 23 October 2013

Some exercises with the "Caesar cipher" Pt 2.

Plaintext    |a |b |c  |d |e |f  |g |h |i   |j | k | l  | m| n| o | p | q  | r  | s | t  | u | v | w | x | y | z |
P-Number  |0 |1 |2 | 3 |4 |5 |6 |7 |8 |9 |10|11|12|13|14|15 |16 |17|18|19|20|21|22 |23|24|25|
Ciphertext |d |e | f | g |h |i  |j  |k |l |m | n | o | p | q | r  | s| t  | u | v | w | x | y | z  | a | b  | c |

In the Caesar cipher, the modulus operator was used to achieve a fast way of computing the result.
The use of an array from 0 to 25 is inherent to the discrete implementation methods widely used. Look-up tables also get this particularity many a times !
If it is not obvious why, wait until one of the next posts on the subject. Other examples will surely make obvious why .
Let's remember  what was said in a previous post :

// Caesar cipher ==> C = (P+s) Mod (Number_of_Characters)
// Number_of_Characters also known as Keyspace = 26 (0 to 25)
// P = numerical equivalent of character plaintext
// C = Numerical equivalent of cipher text character
// s = number of shifts,adds (n_shift)
// Ke = s = 3 ==> (P+s) mod (Number_of_Characters)
// Kd = -s = -3 ==> (C -s)mod (Number_of_Characters) (reverse operation)

From this, we can then adapt this idea into the way the ascii numerical code uses to represent printable characters.

// Keyspace = 96 ( Nchars; )
// P = retrieved
// s =  n_shift
// [ 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; ==> "~" Equivalence sign - tilde

The way i found was to first subtract  an ascii_offset (retrieved -=32;) compute the algorithm ( uint16_t c =(retrieved + n_shift)% Nchars; ) and then re-add the ascii_offset (retrieved +=32;).

C = (P+s) Mod ( Keyspace )

I should probably note the fact that it should not be confused with the remainder.
Let us see why...

If in C = A modulus B ,  A=13 and B=5 , then C=3 ;
If    C = A remainder B, then it is also C=3.

BUT (and there are always but's) what if we give A=13 and B= -5 ?!
Then, we would get A Modulus B ==> C= -2; In turn, A Remainder B ==> C=3 !!

Oh, yeah !! The art of the Modulus operator in itself  ( specially with only the use of a hand calculator), and its relationship with cryptography, is an art in itself , and can easy allow for a series of posts elaborating on it !
As an example, check the table below (created with Gnumeric) .



Code here
https://docs.google.com/document/d/1CbK-v1OaQIhbr0ao5EY8CrGZ71M9-t4cav4bK4JBwo0/edit?usp=sharing

Tuesday, 22 October 2013

Some exercises with the "Caesar cipher"

The Caesar cipher is an example of character or monographic substitution cipher., as it enciphers by substituting a single (mono) character for other single ones.

in the previous related post, the plaintext gives us :

plaintext to encipher- Arduino to cipher this#
Ciphered_array- Dugxlqr#wr#flskhu#wklv&

* you can use the ascii table link for reference.

The Caesar cipher is named after Julius Caesar, who, according to Suetonius, used it with a shift of three to protect messages of military significance.It is unknown how effective the Caesar cipher was at the time, but it is likely to have been reasonably secure, not least because most of Caesar's enemies would have been illiterate and others would have assumed that the messages were written in an unknown foreign language(1)

Of course we can use other shifts; Hal in Arthur C. Clarke's "2001: A Space Odyssey" was chosen as the name for the computer, as it reads 'IBM' with a forward shift of 25 or a back shift of 1 !! Done in a normal 26 letter-only alphabet characters (basic original form)(2).
*There are other funny facts about Hal, actually: the fact that Kubrick is said to have had problems with the initial  use of IBM logos, who decided to withdraw them, due to their disapproval of a computer killing people and how that would look like for them, in terms of publicity !

Polemics aside...
In cryptography the use of modulus operator is essential. An operation not so easy ( read quick) to do with a calculator I intend to do a whole post on that, as "i love me some maths" !
Not much to say about the Caesar cipher, as most of us tried it as kids at some point. Another widely spread use of it was creating a paper set of double wheels( see below pic).

It is as easy as A becomes D( 3 shifts to the right).

Of course this number of shifts can be increased/decreased, and do all sorts of variations in the enciphering key! 
So an implementation of the Caesar cipher algorithm would have to be described as :

C=(P+s) Mod (Number_of_Characters) 
Number_of_Characters= 26 (also known as keyspace; 26 in the case of low case alphabet only)
 P = numerical equivalent of character plaintext  
 C = Numerical equivalent of ciphertext character
 s = number of shifts/adds 
Ke== Enciphering Key
Kd==Deciphering Key
*The last two are termed the keys of a cryptosystem.
Kerchoffs' principle enunciates that the security of a cryptosystem must not depend on keeping the cryptographic algorithm secret, but only in keeping the KEYS secret.
 Ke= s = 3 ==> (P+s) mod (Number_of_Characters)
 Kd = -s = -3 ==> (C -s)mod (Number_of_Characters)


Check Arduino Implementation post here 

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