Understanding Cryptography

Ahmad Syed
3 min readOct 22, 2020

Ceasars’ Cipher

The modulo operator is one of the more useful mathematical tools in Python. One such areas where this tool shines through is when you try to code a simple Caesar cipher.

A Ceasar cipher is a message decoding method that shifts the alphabet by a certain number. For example, say we set this number, known as the key, to +3. In this case the letters in the message ‘a red cat’ is shifted by three letters. This would mean the letter ‘a’ would be changed to three letters over to a ‘d’, and so on for the rest of the letters until we get ‘d uhg fdw’.

This type of cipher becomes useful in coding as it can translate long messages in an instant. However, the code has to know exactly which characters to translate along with not breaking once it comes across a space. For this reason, we list out the letters and punctuation along with for loop.

alphabet = "abcdefghijklmnopqrstuvwxyz"
punctuation = ".,?'! "
message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!"
translated_message = ""
for letter in message:
if not letter in punctuation:
letter_value = alphabet.find(letter)
translated_message += alphabet[(letter_value + 10) % 26]
else:
translated_message += letter
print(translated_message)

In this example we iterate over the message string. In the if statement, we translate only if the current character is NOT within punctuation string, and we skip over the spaces in the else statement.

The same logic applies for decoding a message, however certain guesswork is needed. That is why it is wise to make these blocks of code into a function wherein you can iterate over every key.

def decoder(message, offset):
translated_message = ""
for letter in message:
if not letter in punctuation:
letter_value = alphabet.find(letter)
translated_message += alphabet[(letter_value + offset) % 26]
else:
translated_message += letter
return translated_message
message_one = "jxu evviuj veh jxu iusedt cuiiqwu yi vekhjuud."
print(decoder(message_one, 10))

The Vigenère Cipher

Even with this little hiccup, a Ceasar cipher is far too easy for both human and computer. The Vigenère Cipher is one that is one step up in difficulty.

This Cipher takes a message and decodes every letter to a pattern of keys. For example, the message ‘a red cat’ can be shifter to a patter of ‘39’ over the message. This would mean ‘a’ would be shifter three letters, ‘r’ shifted nine letters, ‘e’ shifted three letters and so on. However, this key would be represented in letters, so in this case it would be represented as ‘ci’.

To code this we need to follow a few steps. First, we need to loop over the message to make a string from the message that would read ‘c ici cic’ in our example. From there we would convert this string into numbers and also convert the message into number with another string so we can shift it. Lastly, we convert the number back into letters. The code would look like this.

def vigenere_decoder(coded_message, keyword):
letter_pointer = 0
keyword_final = ''
for i in range(0,len(coded_message)):
if coded_message[i] in punctuation:
keyword_final += coded_message[i]
else:
keyword_final += keyword[letter_pointer]
letter_pointer = (letter_pointer+1)%len(keyword)
translated_message = ''
for i in range(0,len(coded_message)):
if not coded_message[i] in punctuation:
ln = alphabet.find(coded_message[i]) - alphabet.find(keyword_final[i])
translated_message += alphabet[ln % 26]
else:
translated_message += coded_message[i]
return translated_message

message = "dfc aruw fsti gr vjtwhr wznj? vmph otis! cbx swv jipreneo uhllj kpi rahjib eg fjdkwkedhmp!"
keyword = "friends"

print(vigenere_decoder(message, keyword))

The first for loop accomplishes the first few tasks and the second for loop covers the last few tasks as stated above. As with the Caesar cipher, the coding of a message would be similar to decoding it.

For those of you who want to see the whole program, refer to my github repository of it here https://github.com/AhmadSyed96/Cyphers

--

--

Ahmad Syed
0 Followers

Ahmad Syed, Aspiring Data Scientist