Cryptography With An Interpreter

- - Python, Technology, Tutorials, Web
Hey Guys, it’s been a long time since I published my last article. Apologies for the delay. Anyway, straight into the topic, Cryptography. Well, cryptography with python. This semester(V of Bsc CS) I choose Cryptography as an elective over Neural Network and I am enjoying it. So far I have learned Ceasar Cipher, Playfair Cipher, Vigenere Cipher, Vernom Cipher, Hill Cipher and Rail Fence Cipher. It’s just the beginning anyway. I have implemented these learnings via python programming language. Here is link to the github repository containing the implementations. The repo will be updated as I go further into learning throughout the semester.

Quick walk

Cryptography is the science that is used to hide the information. Mathematically defined as a tuple of five members (M, C, E, D, K) where,

M → Message (Plain Text)

C → Cipher Text

K → Set of Keys

E → Encryption Algorithm E: M*K → C

D → Decryption Algorithm D: C*K → M

On the other hand, cryptanalysis is the study of cipher systems with the view of finding the weaknesses in them that will enable us to retrieve the plain text.

Furthermore, ciphers can be classified into various types based on their properties. Well cipher in general is an algorithm to perform encryption and decryption. We could group Substitution and Transposition into Historical ciphers. Substitution cipher may further be monoalphabetic or polyalphabetic. Modern ciphers are either based on input data or based on key. Stream cipher and Block cipher are the types of modern cipher based on input data. Based on key there are symmetric(private) and asymmetric(public) ciphers.

Following are one of the implementations of ciphers I’ve learned so far. For complete package, follow this link to the repository.

Vernom Cipher

#sample plain text : hello
#sample key : axhjb

def make_chunks(text, text_length, key_length):
    for i in range(0, text_length, key_length):
        yield text[i : i + key_length]


def encryptdecrypt(cipher_generator):
    final_text = ""
    for item in cipher_generator:
        for i in range(0, len(item)):
            final_text += alphabets[alphabets.index(key[i]) ^ alphabets.index(item[i])]
    return final_text

alphabets = "abcdefghijklmnopqrstuvwxyz"

plain_text = raw_input("Enter the plain text: ")

key = raw_input("Enter the key: ")

plain_text = plain_text.replace(" ", "")

p_generator = make_chunks(plain_text, len(plain_text), len(key))

cipher_text = encryptdecrypt(p_generator)

print "The cipher text is : ", cipher_text

c_generator = make_chunks(cipher_text, len(cipher_text), len(key))

decrypted_text = encryptdecrypt(c_generator)

print "The decrypted text is : ", decrypted_text


Tell me how you felt the article was in the comments section below or shoot me a message at bbhishan@gmail.com . And there is always thanks for reading. Cheers ????

Bhishan Bhandari [22] Brewing contents directly from the Himalayas of Nepal. I am a hobbyist programmer and enjoy writing scripts for automation. If you'd like a process to be automated through programming, I also sell my services at Fiverr . Lately, I like to refresh my Quora feeds. Shoot me messages at bbhishan@gmail.com  

Leave a Reply

Your email address will not be published. Required fields are marked *