The Caesar cipher is one of the simplest encryption algorithms in which every latin letter of a given string is simply shifted cycliacally by a certain offset.

For cracking the encryption, we could iterate over all opportunities and as our alphabet uses just 26 latin letters, we would obtain the decrypted string in at most 25 tries, which is quite trivial. An example of the Caesar cipher is rot13 (rotate by 13 places) in which the alphabet is rotated by exactly the halve alphabet. A second encryption would result in the original string used by the first encryption.

I had the idea to crack the encryption on base of an analysis of the quantity of every letter and to obtain the used key this way. This is possible, because our alphabet doesn't have a uniform distribution of every single letter.

latin letter distribution

For cracking the whole thing, an algorithm must simply find the smallest distance beween the encrypted and every decrypted string. I've written a decrypter to crack any Caesar cipher and to obtain the used key by simply guessing the right answer. First we will implement an algorithm to encrypt a string using Caesar to get a perfect initial situation for our furthermore cracking attempt.. This is relatively easy to implement, we run through a given string and replace each letter by the ($char + $n) % 26. place on it: