Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cipher
3.
Substitution in Cipher
3.1.
Simple substitution
3.1.1.
Example
3.2.
Monoalphabetic substitution
3.2.1.
Example
3.3.
Homophonic Substitution Cipher
3.3.1.
Example
3.4.
Polyalphabetic substitution
3.4.1.
Example
3.5.
Polygraphic substitution
3.5.1.
Example
3.6.
Caesar Cipher
3.6.1.
Example
3.7.
Transposition Cipher
3.7.1.
Example
4.
Encryption and Decryption Formula
5.
Frequently Asked Questions
5.1.
What is Cryptography?
5.2.
What are the goals of Cryptography?
5.3.
What are Ciphers?
5.4.
What are plain text and ciphertext?
5.5.
What are encryption and decryption?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Substitution in Cipher

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Around 100 BC, Julius Caesar used to send secret messages to his ministers in the war field using a cryptography method that gave him a tremendous strategic advantage. 

Substitution in Cipher

Even if his messages got intercepted during the event, his enemies could not read them. This oldest cryptography method, named after Julius Caesar, is today known as 'Caesar Cipher.' So are you also wondering what the code that helped him to hide the messages from enemies was?  

Cipher img

Let's now compare this situation to today's era. Nowadays, everyone wants their data to be secured. Cryptography is the process of hiding information. Many cipher algorithms are used for encrypting and decrypting the data. 

Before diving deep into the Substitution in Cipher, let's first quickly understand what a cipher is.

Cipher

A cipher, also called an encryption algorithm, is a method for encrypting and decrypting data. 

cipher img

A cipher converts the original message into ciphertext using a key. This encrypted, unreadable text is known as ciphertext. The study of cryptographic techniques is known as cryptology or cryptography.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Substitution in Cipher

In cryptography, ciphertext substitution in cipher is done in a defined manner using the cipher key. These ciphertexts may be single letterspairs of letterstriplets of letters, or mixtures of the above. On the other side, the receiver deciphers or decrypts the text by performing the inverse substitution to extract the plain original text

There are different types of substitution in cipher, such as-

Simple substitution

In a simple substitution cipher, the cipher operates on single letters. It is the method of substituting every plain text character for every cipher text character. The sender and the receiver decide on a randomly selected permutation of the letters of the alphabet. In this process, alphabets are jumbled compared to the Caesar cipher algorithm. 

Simple Substitution

Example

Keys for a simple substitution cipher usually consist of 26 letters. An example key is given below −

Plain Alphabet

a b c d e f g h i j k l m n o p q r s t u v w x y z

 

Cipher Alphabet

q w e r t y u i o p a s d f g h j k l z x c v b n m

 

An example encryption using the above key is−

Plain Text

Welcome to Coding Ninjas!

Cipher Text

 vtsegdt zg egrofu fofpql

On receiving the ciphertext, the receiver, who also knows the randomly chosen permutation, replaces each ciphertext letter on the bottom row with the corresponding plaintext letter in the top row. 

Monoalphabetic substitution

In monoalphabetic cipher, we use fixed substitution over the entire message. For this cipher, the key is the whole table. For decryption also, we will use the same key. Let's take an example for better understanding. 

Example

Let’s consider we randomly assigned one variable out of the 26 letters to A. Now B will be one of the remaining 25 letters. Similarly, C will also be assigned one of the remaining 24 letters, and so on. This way, we will create an alphabet table similar to this. 

Plain Alphabet

a b c d e f g h i j k l m n o p q r s t u v w x y z

Cipher Alphabet

q w e r t y u i o p a s d f g h j k l z x c v b n m

An example encryption using the above key is−

Plain Text

Welcome to Coding Ninjas!

Cipher Text

 vtsegdt zg egrofu fofpql

This is called a mono-alphabetic substitution cipher. For this cipher, there is no equivalent addition for encrypting the plaintext. The key is the whole table, and each letter is substituted by the key character. Decryption also uses the same key.

Homophonic Substitution Cipher

The Homophonic substitution and mono-alphabetic substitution are very much alike. In Homophonic Substitution, plaintext letters are mapped to more than one alphabet or a ciphertext symbol. 

Example

A

X, O, G, K, 1, 3, 6

B

V, L, H, N, 4, 8

C

A, P, B, W, 0, 2

D

Q, E, W, C, 7, 5

A can be replaced with X, O, G, K, 1, 3, 6 and B will replace with V, L, H, N, 4, 8, etc. The Homophonic Substitution cipher is a substitution cipher in which single plaintext letters can be replaced by any of several different ciphertext letters.

Polyalphabetic substitution

Polyalphabetic cipher, also known as Vigenère cipher, uses a word or random string of letters for substitutions at different positions in the message. The algorithm cycles back to the beginning of the key after using the last letter in the key and starts again, and this undergoes until it reaches the last letter in the message.

Vigenère cipher img

Example

Key Character

ninja

Plain Alphabet

a

b

c

d

e

f

g

h

i

j

k

l

m

1

2

3

4

5

6

7

8

9

10

11

12

13

n

o

p

q

r

s

t

u

v

w

x

y

z

14

15

16

17

18

19

20

21

22

23

24

25

26

Cipher Alphabet

a b c d e f

g

 

h i j k l m n o p q r s t u v w x y z
n j p m e s o u r j x t z w o c y e b t h d j g y m

An example encryption using the above key is−

Plain Text

coding, ninjas, best, growth

Key

ninja

Cipher Text

pwqrnt, vvwjna, onsg, oexwgp

Use the letters c+n=p for the first letter, o+i=w for the second, d+n=q for the third, and so on to encrypt. Once the key ends, return to the beginning of the key. 

Polygraphic substitution

A polygraphic (block) cipher operates on larger groups of letters. Several letters (or digits) are encrypted simultaneously using a method that can handle all possible combinations of a set amount of characters.

Example

 

1

2

3

4

5

1

A

B

C

D

E

2

F

G

H

I/J

K

3

L

M

N

O

P

4

Q

R

S

T

U

5

V

W

X

Y

Z

Caesar Cipher

Caesar cipher or a shift cipher is a simple example of a substitution cipher. Caeser shifted each letter of his message three positions right( change to 3rd next alphabet) to produce what could be called ciphertext. If the enemy somehow got the message, he will see the ciphertext instead of the true message. For example, in Caeser's message letter 'A' would become 'D,' letter 'B' would become 'E,' and the letter 'Y' becomes 'B.' Caser used the key as 3.

Example

The above table depicts the value x for each letter. The entire mapping would look like this

Plain Alphabet

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Cipher Alphabet

D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

An example encryption using the above key is−

Plain Text 

WELCOME TO CODING NINJA!

Cipher Text

ZHOFRPH WR FRGLQJ QLQMD!

Transposition Cipher

It is another type of cipher where the order of the alphabets in the plaintext is rearranged to create the ciphertext. The actual plaintext alphabets are not replaced.

An example is a ‘simple columnar transposition’ cipher where the plaintext is written horizontally with a certain alphabet width. Then the ciphertext is read vertically as shown.

Example

An example of encryption using the above key is−

Plain Text 

welcome to coding ninjas keep hustling

Secret Random Key

six

We arrange this text horizontally in the table with a number of columns equal to the key value. The resulting cipher text is given below.

Cipher Text

w

e

l

c

o

m

e

t

o

c

o

d

i

n

g

n

i

n

j

a

s

k

e

e

p

h

u

s

t

l

i

n

g

     

The ciphertext is obtained by reading the column vertically downward from the first to the last column. The ciphertext is ‘weijpietnahnlogsugccnksooietmdnel’.

To decrypt, the receiver writes the received ciphertext vertically down and from the left to the right column. To obtain the text, he reads horizontally from left to right and from top to bottom row.

Encryption and Decryption Formula

 

The formula of encryption is:

En (x) =(x + n) mod 26

 

The formula of decryption is:

Dn(x) = (x - n) mod 26

 

Where, 

En depicts encryption

Dn depicts decryption

x denotes the value of the letter

n denotes the shift or key-value

If any case (Dn) value becomes negative (-ve), in this case, we will add 26 to the negative value.
 

Cheers if you reached here!! 

Frequently Asked Questions

What is Cryptography?

Cryptography uses complex techniques and procedures to hide information while sending, storing, and processing data. 

What are the goals of Cryptography?

Confidentiality, Integrity, Availability, and Non-Repudiation of IT systems are the goals of cryptography.

What are Ciphers?

Ciphering is the process of transforming data into an unreadable format. In other words, it is an algorithm responsible for data encryption and decryption. 

What are plain text and ciphertext?

Plaintext refers to anything humans can understand, whereas ciphertext is a series of randomized letters, symbols, and numbers humans cannot understand. 

What are encryption and decryption?

Encryption is the conversion of plain text into ciphertext. In contrast, decryption is the process that converts ciphertext into plaintext.

Conclusion

In this article, we have extensively discussed cipher and the different types of substitution in cipher with their examples. To learn more about the substitution in cipher, check the link below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Previous article
Caesar, Shift the Cipher
Next article
Special thy Affine Cipher
Live masterclass