DEV Community

Darshan Sahadev Gawade
Darshan Sahadev Gawade

Posted on

Monoalphabetic Cipher

Monoalphabetic cipher is one where each character of a plain text is mapped to a fixed other character of cipher text. The relationship between a character in the plain text and the characters in the cipher text is one-to-one.

Example : if a plain text has a character ‘a’ and any key then if it convert into other character say ‘t’ so wherever there is ‘a’ character in plain text it will be mapped to character ‘t’ ,Therefore it is called as monoalphabetic cipher.

It is a simple type of substitution cipher. Monoalphabetic ciphers are not that stronger as compared to polyalphabetic cipher.

Alt Text

Types of monoalphabetic cipher are

  • Additive Cipher
  • Caesar Cipher
  • Multiplicative Cipher
  • Affine Cipher

Additive cipher

Additive cipher is the type of monoalphabetic substitution cipher, in which the each character of a plain text is mapped by some other character depending upon the value of key.

Example: If the plain text contain alphabet 'B' and the value of key is '4', then the alphabet 'B' will be replaced by the alphabet 'F' i.e the 4th alphabet after 'B' .

Mathematical Representation is

Encryption process :
C=( P + k) mod 26
where, 'P' is the character in plain text, 'K' is the key and 'C' is the required cipher

Decryption process :
P=( C - k) mod 26

Python program for Encryption and Decryption process :

# Encryption part
def encrypt(message, key):
    cipher = ""
    for i in message:
        if i.isupper():
            cipher += chr((ord(i) + key - 65) % 26 + 65)
        elif i.islower():
            cipher += chr((ord(i) + key - 97) % 26 + 97)
        else:
            cipher+=" "

    return cipher

message = input("Enter the message:")
key = input("Enter the key numeric value or any alphabet:")
if key.isupper():
    key = ord(key) - 65
elif key.islower():
    key = ord(key) - 97
else:
    key = int(key)
print("Cipher:", encrypt(message, key))

# Decryption part
def decrypt(cipher, key):
    message = ""
    for i in cipher:
        if i.isupper():
            message += chr((ord(i) - key - 65) % 26 + 65)
        elif i.islower():
            message += chr((ord(i) - key - 97) % 26 + 97)
        else:
            message+=" "
    return message


cipher = input("Enter the cipher:")
key = input("Enter the key numeric value or any alphabet:")
if key.isupper():
    key = ord(key) - 65
elif key.islower():
    key = ord(key) - 97
else:
    key = int(key)
print("Message", decrypt(cipher, key))

OUTPUT :
Enter the message:hello everyone
Enter the key numeric value or any alphabet:19
Cipher: axeeh xoxkrhgx
Enter the cipher:axeeh xoxkrhgx
Enter the key numeric value or any alphabet:19
Message: hello everyone

Caesar Cipher

Caesar cipher is the most simplest form of cipher, it is similar to additive cipher .In caesar cipher the value of key is always '3'.

Mathematical Expression is

Encryption process
C=(P + K) mod 26
where, 'P' is the character in plain text, 'K' is the key (k=3) and 'C' is the required cipher

Decryption process
P=(C - K) mod 26

Python program for Caesar Cipher

#Additive cipher is similar to caesar cipher ,in caesar cipher key is always '3'
# Encryption part
def encrypt(message, key):
    cipher = ""
    for i in message:
        if i.isupper():
            cipher += chr((ord(i) + key - 65) % 26 + 65)
        elif i.islower():
            cipher += chr((ord(i) + key - 97) % 26 + 97)
        else:
            cipher+=" "

    return cipher

message = input("Enter the message:")
print("Cipher:", encrypt(message, 3))

# Decryption part
def decrypt(cipher, key):
    message = ""
    for i in cipher:
        if i.isupper():
            message += chr((ord(i) - key - 65) % 26 + 65)
        elif i.islower():
            message += chr((ord(i) - key - 97) % 26 + 97)
        else:
            message+=" "
    return message

cipher = input("Enter the cipher:")
print("Message: ", decrypt(cipher, 3))

OUTPUT:
Enter the message:hello everyone
Cipher: khoor hyhubrqh
Enter the cipher:khoor hyhubrqh
Message: hello everyone

Multiplicative Cipher

In multiplicative cipher, character of a plain text is multiplied with the key and then modulus function is applied on it. It is a type of monoalphabetic substitution cipher hence it is not a stronger cipher.

Mathematical Expression

Encryption process
C=(P * K) mod 26
where, 'P' is the character in plain text, 'K' is the key and 'C' is the required cipher

Python program for Multiplicative Cipher

#Encryption
def check(c):
        if c.isupper():
                c=ord(c)-65
        elif c.islower():
                c=ord(c)-97
        else:
                c=int(c)
        return c

def encrypt(message,k):
        cipher=""
        for i in message:
                if i.isupper():
                        cipher+=chr(((ord(i)-65)*k)%26+65)
                elif i.islower():
                        cipher+=chr(((ord(i)-97)*k)%26+97)
                else:
                        cipher+=" "
        return cipher

message=input("Enter the message:")
k=input("Enter the keys, numeric value or any alphabet:")
k=check(k)

print("Cipher:",encrypt(message,k))

#Decryption part
def getCoeff(d):
        for i in range(1,26):
                j=int(1)
                eqn=int(1)
                while(eqn>=1):
                        eqn=26*i-d*j
                        if eqn==1:
                                return -j
                        j=j+1

def decrypt(cipher,k):
        message=""
        k=getCoeff(k)
        for i in cipher:
                if i.isupper():
                        message+=chr(((ord(i)-65)*k)%26+65)
                elif i.islower():
                        message+=chr(((ord(i)-97)*k)%26+97)
                else:
                        message+=" "
        return message

cipher=input("Enter the cipher:")
k=input("Enter the key, numeric value or any alphabet :")
k=check(k)

print("Message:",decrypt(cipher,k))

OUTPUT :
Enter the message:hello everyone
Enter the keys, numeric value or any alphabet:3
Cipher: vmhhq mlmzuqnm
Enter the cipher:vmhhq mlmzuqnm
Enter the key, numeric value or any alphabet :3
Message: hello everyone

Affine Cipher

Affine cipher is the stronger cipher among the additive and multiplicative cipher. Affine cipher consists of two keys as it a combination of additive and multiplicative cipher .

Mathematical Expression is

Encryption process
C=( P *k1 + k2) mod 26
where, P is the character in plain text, K1 is multiplicative key ,K2 is additive key ,C is the character in cipher.

Decryption process
P=( (C- k2 ) / k1 ) mod 26

Python program for Affine Cipher .

#Encryption
def check(c):
        if c.isupper():
                c=ord(c)-65
        elif c.islower():
                c=ord(c)-97
        else:
                c=int(c)
        return c

def encrypt(message,a,b):
        cipher=""
        for i in message:
                if i.isupper():
                        cipher+=chr(((ord(i)-65)*a+b)%26+65)
                elif i.islower():
                        cipher+=chr(((ord(i)-97)*a+b)%26+97)
                else:
                        cipher+=" "
        return cipher

message=input("Enter the message:")
#’a’ is multiplicative key
#’b’ is additive key
a,b=input("Enter the two keys, numeric value or any alphabet separated spaces:").split()
a=check(a)
b=check(b)

print("Cipher:",encrypt(message,a,b))

#Decryption part
def getCoeff(a):
        for i in range(1,26):
                j=int(1)
                eqn=int(1)
                while(eqn>=1):
                        eqn=26*i-a*j
                        if eqn==1:
                                return -j
                        j=j+1

def decrypt(cipher,a,b):
        message=""
        a=getCoeff(a)
        for i in cipher:
                if i.isupper():
                        message+=chr(((ord(i)-65-b)*a)%26+65)
                elif i.islower():
                        message+=chr(((ord(i)-97-b)*a)%26+97)
                else:
                        message+=" "
        return message

cipher=input("Enter the cipher:")
a,b=input("Enter the two keys, numeric value or any alphabet seperated by spaces:").split()
a=check(a)
b=check(b)

print("Message:",decrypt(cipher,a,b))

OUTPUT :
Enter the message: hello everyone
Enter the two keys, numeric value or any alphabet separated spaces:3 5
Cipher: armmv rqrezvsr
Enter the cipher: armmv rqrezvsr
Enter the two keys, numeric value or any alphabet seperated by spaces:3 5
Message: hello everyone

Top comments (0)