Cryptography is the art of science which converts the readable text message to unreadable format. There are various algorithms in python to implement on same and two of them which is used majorly is mentioned below:
1. Monoalphabetic Cipher
2. Polyalphabetic Cipher
Monoalphabetic Cipher:
A monoalphabetic cipher algorithm uses fixed substitution over the entire plain text message to convert to cipher text message.
The JSON representation of monoalphabetic substitution is given below:
monoalpha_cipher = {
‘a’: ‘m’, ‘b’: ‘n’, ‘c’: ‘b’, ‘d’: ‘v’, ‘e’: ‘c’, ‘f’: ‘x’, ‘g’: ‘z’, ‘h’: ‘a’, ‘i’: ‘s’, ‘j’: ‘d’, ‘k’: ‘f’, ‘l’: ‘g’, ‘m’: ‘h’, ‘n’: ‘j’, ‘o’: ‘k’, ‘p’: ‘l’, ‘q’: ‘p’, ‘r’: ‘o’, ‘s’: ‘i’, ‘t’: ‘u’, ‘u’: ‘y’, ‘v’: ‘t’, ‘w’: ‘r’, ‘x’: ‘e’, ‘y’: ‘w’, ‘z’: ‘q’, ‘ ‘: ‘ ‘, } |
The Python code for implementing on monoalphabetic cipher algorithm is mentioned below. This code is best suitable for Python 2.7.
Step 1: Import the necessary modules
from string import letters, digits
from random import shuffle |
Step 2: Create random generated numbers and alphabets corresponding that numbers.
def random_monoalpha_cipher(pool=None):
“””Generate a Monoalphabetic Cipher””” if pool is None: pool = letters + digits original_pool = list(pool) shuffled_pool = list(pool) shuffle(shuffled_pool) return dict(zip(original_pool, shuffled_pool)) |
Step 3: Create a function through which given a monoalphabetic cipher we can create an inverse on the same.
def inverse_monoalpha_cipher(monoalpha_cipher):
inverse_monoalpha = {} for key, value in monoalpha_cipher.iteritems(): inverse_monoalpha[value] = key return inverse_monoalpha |
Step 4: Create a function for encryption and decryption functions which implements the functionality of monoalphabetic cipher.
def encrypt_with_monoalpha(message, monoalpha_cipher):
encrypted_message = [] for letter in message: encrypted_message.append(monoalpha_cipher.get(letter, letter)) return ”.join(encrypted_message) def decrypt_with_monoalpha(encrypted_message, monoalpha_cipher): return encrypt_with_monoalpha( encrypted_message, inverse_monoalpha_cipher(monoalpha_cipher) ) |
Now, we will focus on the implementation of monoalphabetic cipher with Python version 3+ which is demonstrated below for better understanding:
import random
alpha = “abcdefghijklmnopqrstuvwxyz” #Encrypts the plain text message def encrypt(original, key=None): if key is None: l = list(alpha) random.shuffle(l) key = “”.join(l) new = [] for letter in original: new.append(key[alpha.index(letter)]) return [“”.join(new), key] #Decrypts the encrypted message def decrypt(cipher, key=None): if key is not None: new = [] for letter in cipher: new.append(alpha[key.index(letter)]) return “”.join(new)
e = encrypt(“monoalphabetic”, None) print(e) #Prints encrypted message print(decrypt(e[0], e[1])) #Decodes the message and prints it |
Output:
C:\Users\DELL\Desktop\ >python monoalphabetic-cipher-encryption.py
[‘thyhrjslrqaneg’, ‘rqgcawvlezpjtyhsfiunkbodxm’] monoalphabetic |
Polyalphabetic Cipher:
The best illustration of polyalphabetic cipher is Vigenere Cipher which is explained below:
When developers talk naive ciphers, the Vigenere cipher is likely considered to be most secure cipher encryption. It is build on the principle of the Caesar cipher which includes a decent way of providing easy solution to solve shift problems. The basic gist of this cipher is that the user should have a message and a key. The key can be any length, but user must be aware that key can be repeated with respect to the length of message. The demonstration is given below:
alpha = ABCDEFGHIJKLMNOPQRSTUVWXYZ
message = IAMTHEWALRUS #Plain text
key = HELLOHELLOHE #key
Our key is “HELLO”, but when expanded it to the length of our message giving us the repeated nature we see. Once we have these defined characters, we substitute the character by character performing a pseudo-Caesar cipher.
m1 = I = 9
k1 = H = 8
9 + 8 = 17 % 26 = 17 = Q
c1 = Q
Looking at this, we see that the first character of our message is “I” which is the ninth letter in the alphabet. We then look at the first character of the key which is “H” or the eighth letter in the alphabet. We also add these two numbers and modulo 26 giving us 17 which points to the seventeenth letter in the alphabet: “Q”. We now know that the first letter of our ciphertext is “Q”.
Now you can repeat this method for each character in our message until we have the ciphertext. This can be better shown as the algorithm:
Let m be our message and k be our key:
E(m) = ((m1 + k1) % 26, (m2 + k2) % 26, …, (mi + ki) % 26)
D(m) = ((c1 – k1) % 26, (c2 – k2) % 26, …, (ci – ki) % 26)
This is much like the Caesar cipher except instead of defining a fixed rotation, we allow our key’s character index to be the rotation. It is observed that cipher can be considered a string of Caesar ciphers. The implementation of polyalphabetic cipher (vigenere cipher) is mentioned below:
def encrypt(plaintext, key):
key_length = len(key) key_as_int = [ord(i) for i in key] plaintext_int = [ord(i) for i in plaintext] ciphertext = ” for i in range(len(plaintext_int)): value = (plaintext_int[i] + key_as_int[i % key_length]) % 26 ciphertext += chr(value + 65) return ciphertext
def decrypt(ciphertext, key): key_length = len(key) key_as_int = [ord(i) for i in key] ciphertext_int = [ord(i) for i in ciphertext] plaintext = ” for i in range(len(ciphertext_int)): value = (ciphertext_int[i] – key_as_int[i % key_length]) % 26 plaintext += chr(value + 65) return plaintext print(“Encrypted message”,encrypt(“hello”,“hi”)) |
Output:
C:\Users\DELL\Desktop\ >python vignereCipher.py
Encrypted message AYEFH |
Conclusion:
Caesar Cipher is an example of Mono-alphabetic cipher, as single alphabets are encrypted or decrypted at a time. Monoalphabetic ciphers are stronger than Polyalphabetic ciphers because frequency analysis is tougher on the former. The best illustration of polyalphabetic cipher is Vigenere Cipher encryption. Vigenere Cipher uses a simple form of polyalphabetic substitution. A polyalphabetic cipher is considered as cipher-based substitution, using multiple substitution alphabets. This is usually possible with Vigenere Cipher table.