Whenever I learn something, I always try to implement it in a more familiar space, so as to give context to understand it better. In this case, I am implementing concepts from Cryptography into Python to understand it better.
For those of you who are familiar with Python, I hope that you can learn a little bit of Cryptography from this article and for those of you who are familiar with Cryptography, I hope you can learn some a Python. For those who are nor familiar with Python, I envy you as this is the perfect learning opportunity to learn about both at the same time.
Ciphers are the backbone of Cryptography, the foundation of how messages can be encoded so as for the true message to be transmitted for those who it was intended to. I find this fascinating because before this, one could have only used physical means to hide information from others.
A cipher functions as so:
Cipher(Plaintext) = Ciphertext
The Cipher is a function that encodes the message so that the original text cannot be derived from the Cipher-text directly.
To get from the Ciphertext to the Plaintext:
Plaintext = Cipher^-1(Ciphertext)
We have to take the inverse of the Cipher function to reach the original message.Let’s look at one type of cipher:
In a Caesar Cipher, we are shifting the alphabet forward or background by a factor of n.
For example, if n = 1, A becomes B, B becomes C, C becomes D and so on. Let’s construct an algorithm for this.
First we want to create a class for all the ciphers:
class ciphers:
def __init__(plaintext):
self.plaintext = plaintext
Next we add the Caesar Cipher:
def caesar(self,n):
words = self.plaintext.split()
alpha = []
indexes = []
ciphertext = []
letters = string.ascii_letters[:26]
for i in range(len(letters)):
alpha.append(letters[i])
for word in words:
for letter in word:
index = alpha.index(letter)
new_index = index+n
if new_index > len(alpha):
new_index - 26
indexes.append(new_index)
for index in indexes:
ciphertext.append(alpha[index])
ciphertext = ''.join(ciphertext)
return ciphertext
#python #cryptography #programming #coding