A Caesar-Cipher is a linear substitution cipher.
Explanation:
Have p be your plaintext.
Have k be our numerical key (<26 for the sake of this explanation).
Have c be one character in p.
Have I(c) be the index of c in p.
Have fc(i) be a function which maps an index i to it's letter in the alphabet.
Have e(c) be the 'encrypted' character of c.
Then:
e(c) = fc( I(c) + k)
Or in plain English: Each characters gets shifted by the value of k. It's therefore a Linear substitution since it always gets shifted by the same amount and the characters are substituted.
The problem with this encryption algorithm is, that you don't really scramble the plaintext or add entropy.
(In harsh words, the Caesar-Cipher is closer to an encoding rather than an encryption).
Given that, we know that all matching letters in our cipher-text substitute for the exact same plain-text character.
This means, you'll be able to run statistical analysis on your Cipher-text and simply analyse the letter distribution. Different sounds/letters occur different amount of times.
So how do we apply this technique?
Based on your name, I'd guess you are German and I'll just assume that your Cipher-text is as well.
In German, the most common letters are E and I.
We're almost there!
Now, you could simply find the most common letter in your cipher-text and calculate the difference between that letter and E (difference between their indexes, of course). That will yield you the secret key!
Here is a code example:
"""
This module aims to break Caesar-ciphers from German plaintext
based on statistics of letter distribution.
The caesar cipher is a character substitution algorithm.
Bob chooses a number as the key n.
Bob then shifts every character of the plain-text by n, cycling
through the entire alphabet.
e.g.: if n = 3: "ABC" -> "DEF".
Since it has a very small keyspace (26^1), it can be easily broken,
and Mallory could even guess or bruteforce the key.
(Note: You could choose a number higher than 26. However, this won't
increase your keyspace since any number x will
be reduced to 1-26. See: (x - (26*(x // 26)).
Common letters in the german language are (in descending order):
"E","N","I","S","R" (subject to verification)
The statistical approach works well for long sentences since one has
a greater samplesize for distribution analysis.
If two or more letters appear the same amount of times, you have to
check which key is actually correct, either
by simply reading the outputs or running them against a
distribution_dict of that language.
"""
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
SPECIAL_CHARS = " ,.-;:_?!="
def encrypt(plain_text, key):
"""
Encrypts plaintext using a caesar.
:param plain_text: The plaintext
:param key: The key (Integer)
:return: The cipher-text
"""
cipher_text = ""
for letter in plain_text:
if letter in SPECIAL_CHARS:
cipher_text += letter
continue
index = ALPHABET.find(letter.upper())
new_index = flatten(index + key)
cipher_text += ALPHABET[new_index]
return cipher_text
def decrypt(cipher_text, key=None):
"""
This function decrypts plaintext. If no key is specified, it
will be found using distribution analysis.
:param cipher_text: The cipher-text
:param key: The key
:return: the plain-text
"""
if key is None:
key = find_key_from_cipher(cipher_text)
plain_text = ""
for letter in cipher_text:
#Skipping special characters (incomplete solution)
if letter in SPECIAL_CHARS:
plain_text += letter
continue
index = ALPHABET.find(letter.upper())
new_index = flatten(index - key)
plain_text += ALPHABET[new_index]
return plain_text
def flatten(number) :
"""
Flattens the key back to be in range(1,26)
:param number:
:return:
"""
return number - (26*(number//26))
def find_key_from_cipher(cipher_text):
index_of_most_common_letter = 4 #Index of 'e'
#Calculate distribution
distribution_dict = analyse_letter_distribution(cipher_text)
#Get common letters
common_letters = sorted(distribution_dict, key=distribution_dict.get, reverse=True)
#Use most common letter to get key
key = ALPHABET.find(common_letters[0].upper()) - index_of_most_common_letter
return key
def analyse_letter_distribution(cipher_text):
distribution_dict = {}
for letter in cipher_text:
if letter in SPECIAL_CHARS:
continue
if letter not in distribution_dict:
distribution_dict[letter] = 1
else:
distribution_dict[letter] += 1
if len(distribution_dict.values()) != len(distribution_dict.values()):
print("Multiple letters appear the same amount of times! Uh oh.")
return distribution_dict
if __name__ == "__main__":
secret = encrypt("This sentence is encrypted. Encryption is broken by using awesome encryption algorithms!",5)
print(decrypt(secret))
One last note: Since it's a statistical analysis, this approach works best if the captured cipher-text is long.
One last last note: This description is a little incomplete (and formatting looks ugly, I'm on mobile.) I'd highly recommend this german book by Klaus Schmeh for further reading.