Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Encryption in Substitution Cipher
1.1.
Procedure
1.2.
Implementation
1.3.
C++
1.4.
Input
1.5.
Output
2.
Decryption Substitution Cipher
2.1.
Procedure
2.2.
Implementation
2.3.
C++
2.4.
Input
2.5.
Output
3.
Frequently Asked Questions
3.1.
What is substitution cipher with an example?
3.2.
What is the most common substitution cipher?
3.3.
What is permutation and substitution cipher?
3.4.
What is the difference between substitution cipher and Caesar cipher?
4.
Conclusion
Last Updated: Jul 15, 2024
Easy

Substitution Cipher

Author Anant Dhakad
0 upvote

A substitution cipher is a method of encryption where each letter in the plaintext is replaced with another letter. This technique has been used for centuries to secure messages, making it one of the oldest and simplest forms of cryptography. In a substitution cipher, the relationship between the plaintext and ciphertext is one-to-one, meaning each character in the plaintext corresponds to a unique character in the ciphertext. 

Substitution Cipher

Encryption is the process of concealing data. When ordinary text is encrypted, it becomes ciphertext, which is unreadable. A substitution cipher replaces any plain text character from a given fixed set of characters with another character from the same set based on a key. With a shift of one, for example, A would be replaced by B, B by C, and so on.

Figure: Substitution Cipher with key=13 (source)

Encryption in Substitution Cipher

Encryption replaces letters with corresponding letters after shifting all letters to the “key” number of positions to the right.

Example:

Plain text: My name is Anant Dhakad

Key: 4

Encrypted text: Qc reqi mw Ererx Hleoeh

Procedure

1. Iterate over all the letters in the input text and replace each letter with a letter “key” positions right to it.

2. Finally, print the encrypted text.

Implementation

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

/* This function encrypts the input text using keyword cipher. */
string substitutionCipher(string text, int key){

   // ciphering the input text;
   for(auto &c: text){
       if(!isalpha(c)) continue;

       int idx = isupper(c) ? c-'A': c-'a';
       idx = (idx + key)%26;

       c = isupper(c) ? char(idx+'A'): char(idx+'a');
   }

   /* Return encrypted text */
   return text;
}

int main(){
   // Taking text input
   string text;
   getline(cin, text);
   cout << "Input Text: " << text << endl;
 
   // Taking key input
   int key;
   cin >> key;
   cout << "Input Key: " << key << endl;

   /* Encrypted the text using Keyword Cipher */
   string encryptedText = substitutionCipher(text, key);
   cout << "Encrypted Text: " << encryptedText << endl;

   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

My name is Anant Dhakad
4

Output

Input Text: My name is Anant Dhakad
Input Key: 4
Encrypted Text: Qc reqi mw Ererx Hleoeh

Decryption Substitution Cipher

Decryption is done by replacing corresponding letters after shifting all letters to the “key” number of positions to the left.

Example:

Input Encrypted text: Qc reqi mw Ererx Hleoeh

Key: 4

Output Decrypted text: My name is Anant Dhakad

Procedure

1. Iterate over all the letters in the input ciphered text and replace each letter with a letter “key” positions left to it.

2. Finally, print the decrypted text.

Implementation

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

/* This function decrypts the input text using keyword cipher. */
string substitutionCipher(string text, int key){

   // ciphering the input text;
   for(auto &c: text){
       if(!isalpha(c)) continue;

       int idx = isupper(c) ? c-'A': c-'a';
       idx = (idx - key + 26)%26;

       c = isupper(c) ? char(idx+'A'): char(idx+'a');
   }

   /* Return decrypted text */
   return text;
}

int main(){
   // Taking text input
   string text;
   getline(cin, text);
   cout << "Input Encrypted Text: " << text << endl;
 
   // Taking key input
   int key;
   cin >> key;
   cout << "Input Key: " << key << endl;

   /* Decrypting the text using Keyword Cipher */
   string decryptedText = substitutionCipher(text, key);
   cout << "Decrypted Text: " << decryptedText << endl;

   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Input

Qc reqi mw Ererx Hleoeh
4

Output

Input Encrypted Text: Qc reqi mw Ererx Hleoeh
Input Key: 4
Decrypted Text: My name is Anant Dhakad

Frequently Asked Questions

What is substitution cipher with an example?

A substitution cipher is an encryption method where each letter in the plaintext is replaced with another letter. For example, in a simple substitution cipher, 'A' might be replaced with 'D', 'B' with 'E', and so on.

What is the most common substitution cipher?

The most common substitution cipher is the Caesar cipher, where each letter in the plaintext is shifted a fixed number of places down the alphabet. For example, with a shift of 3, 'A' becomes 'D', 'B' becomes 'E', etc.

What is permutation and substitution cipher?

Permutation and substitution cipher involves both rearranging the order of characters (permutation) and substituting each character with another character (substitution) to enhance encryption strength.

What is the difference between substitution cipher and Caesar cipher?

A substitution cipher replaces each letter with another specific letter, whereas a Caesar cipher is a type of substitution cipher where each letter is shifted by a fixed number of positions in the alphabet.

Conclusion

In this article, we have extensively discussed Substitution Cipher. Substitution ciphers have played a crucial role in the history of cryptography, offering a simple yet effective way to encrypt messages. Despite their simplicity, they lay the foundation for more complex encryption techniques used today. Understanding substitution ciphers, including their strengths and weaknesses, helps appreciate the evolution of cryptographic methods.

Must read: Cyber Security

We hope that this blog has helped you enhance your knowledge regarding Substitution Cipher and if you would like to learn more, check out our articles on the platform Code 360. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass