If you are the geeky type, like a lot of people I happen to know
(…OK fine, yours truly also) sometime in your childhood you discovered some simple forms of encryption and became fascinated by them to the point where you started creating your own alphabets and childish methods of obscuring texts. And it’s highly probable that one of the first ciphers you discovered was Caesar cipher.
Caesar cipher is a simple substitution cipher where every letter in the plaintext is replaced by another letter in the alphabet shifted a number of positions.
Encryption of a letter x by a shift n can be described as:
En(x)=(x+n) mod 26
For example, if we decide to shift the letters 5 positions:
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 F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
Mathematically you would do this by numbering the letters of the alphabet 0 to 25
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
Transform the letters of the plaintext in numbers:
WE NEED CODE becomes 22 4 13 4 4 3 2 14 3 4
Do the shift for every letter
22 + 5 mod 26 = 1 (B) 4 + 5 mod 26 = 9 (J) 13 + 5 mod 26 = 18 (S) 4 + 5 mod 26 = 9 (J) 4 + 5 mod 26 = 9 (J) 3 + 5 mod 26 = 8 (I) 2 + 5 mod 26 = 7 (H) 14 + 5 mod 26 = 19 (T) 3 + 5 mod 26 = 8 (I) 4 + 5 mod 26 = 9 (J)
The plain message WE NEED CODE becomes BJ SJJI HTIJ
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; |
Notice how I added 26 to the position so that there won’t be any negative values and the PHP modulo would be correct.
Caesar cipher is very easy to crack even by hand: the worst case scenario you try shifting with all numbers from 1 to 25 and stop when the message becomes intelligible. If you don’t do it by hand and instead you use your computer you should be able to crack it almost instantly (granted the encrypted message is not very long).
And as a final word I have a cryptographic riddle for you:
KDLOFDHVDUZHZKRDUHDERXWWRGLHVDOXWHBRX
Good luck!

HailMihaela.
How strange, I got a mis-spelling of “YOU” as “ZOU” when I decoded this. I wonder what I did wrong…
First of all congrats on breaking the code
There will be more riddles to come.
How did you decoded it? Did you write your own code or did you do it by hand?
Maybe you missed a letter at the end of the alphabet (Y is suspiciously close to Z).
If you run my PHP decryption code changing the key from 5 to 3 you get the correct message.
I wrote my own code in c#:
private void Blast()
{
string input_string = @"KDLOFDHVDUZHZKRDUHDERXWWRGLHVDOXWHBRX";
for (int i = 1; i<=26; i++)
Console.WriteLine(AddXToString(input_string, i));
}
private string AddXToString(string input_string, int X)
{
StringBuilder b = new StringBuilder(input_string.Length);
foreach (char c in input_string)
{
char n = (char) ((((((int)c) - 65) + X) % 27) + 65);
b.Append(n);
}
return b.ToString();
}
The key to my encryption was 3, every letter of my initial text was shifted 3 positions.
To decrypt the text you were suppose to shift back 3 positions. Instead you chose to shift forward. That’s good too, you can shift 23 positions forward to get the same results but you mistakenly used modulo 27 instead of 26 (we use modulo 26 because there are 26 letters in the alphabet).
Your code almost worked when shifting 24 positions.
[...] cipher is an encryption technique consisting of a sequence of Caesar cipher each based on the letters of a secret text called a keyword. This mechanism is classified as [...]
Great post, I wrote a Ceaser Cipher also in PHP in a totally different way, it was really inefficient, and kind of crap, yours is great though
x
Mihaela,
I wrote a simple decryption tool using heuristics in order to guess Caesar-Cipher keys. It also finds the correct answer to your “KDLOFDHVDUZHZKRDUHDERXWWRGLHVDOXWHBRX”.
Robert