Recent Posts

Sunday 7 April 2013

0 comments

Caesar Cipher

Caesar Cipher was named after Julius Caesar
In cryptography, Caesar Cipher is one of the simplest and widely used encryption method. It is also know as  Shift cipher or Caesar shift. In this kind of encryption, plain text is replaced by a fixed number of position of the alphabet depending upon the shift key used. For example, if the shift key to be used is 9 then the alphabet A would become J similarly K would become T and so on depending upon the shift key used.
You might be wondering with 9 as a shift key, how A would become J and K would become T? Well, the answer to this question is really very simple. We first encrypted A and it became J because the shift key was 9. Shift key is used to determine the position of alphabet with which our plain text will be replaced with. Taking A=0, B=1 ... Z=26, the position of J will be 9 i.e. J=9 so A will be replaced with the alphabet which is at 9th position.



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
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

Example

Now, we will be using the shift key as 14 to encrypt all 26 alphabets of English language.

Plain text :                          ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted (shift key 14) : OPQRSTUVWXYZABCDEFGHIJKLMN

While encryption, the plain text alphabet is replaced with the corresponding alphabet depending upon the shift key used, decryption is done in reverse. In the below example we will be decrypting a small message which is encrypted using the shift key 2

Cipher text : ygneqog vq vjg dnqi
Plain text    : welcome to the blog


Encryption of a letter x by a shift n can also be described as:
En(x)=(x+n) mod 26


Similarly the decryption would be:
Dn(x)=(x-n) mod 26


PHP code for encryption:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//the text to be encrypted
$plain_text='WENEEDCODE';
 
//letters of alphabet array
$alphabet=array('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');
//positions of the letters in alphabet
$flip=array_flip($alphabet);
 
//plaintext array
$plain_text=str_split($plain_text);
$n=count($plain_text);
$encrypted_text='';
for ($i=0; $i<$n; $i++)
 //encryption
 $encrypted_text.=$alphabet[($flip[$plain_text[$i]]+5)%26];
echo $encrypted_text;
Similarly for decryption we have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//the text to be decrypted
$encrypted_text='BJSJJIHTIJ';
 
//letters of alphabet array
$alphabet=array('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');
//positions of the letters in alphabet
$flip=array_flip($alphabet);
 
//plaintext array
$encrypted_text=str_split($encrypted_text);
$n=count($encrypted_text);
$decrypted_text='';
for ($i=0; $i<$n; $i++)
 //decryption
 $decrypted_text.=$alphabet[(26+$flip[$encrypted_text[$i]]-5)%26];
echo $decrypted_text;
Caesar cipher is very easy to crack manually also, just use your brain a bit ;) So at last I have got a small message for you to decrypt, I encrypted the message with the shift key 17

pfl tirtbvu ! jyriv kyv gfjk reu yvcg lj kf xifn

( Labels: , ) Read more
Best viewed on firefox 5+

Labels

Popular Posts

Copyright © Design by Dadang Herdiana