Implementation of Null Cipher in C++

In this tutorial, we will learn how to implement NULL Cipher using C++.

In null cipher, ‘null’ stands for ‘nothing’ and ‘cipher’ stands for ‘code’. Basically, it is a code in which most of the letters mean nothing at all because they are just there to hide the real ones.

C++ program for encryption using Null Cipher


Basically in this type of encryption technique, we mix the original word with lots of unwanted letters thus making it difficult to understand.

Input -> Code Speedy 

After mixing the input with random cipher materials we get, 
Output -> oCJozX OoGbUA NdCNfe qejjaN mSyNfM OpOGnJ peWERd UeYFTJ edpPfA NySYqN 

This way our message gets encrypted and if we want to decrypt the message just take the second alphabet from each word.

Now, let’s see how it is implemented.




  1. Input a string.
  2. Mixing each character of the string with random alphabets.
  3. Return the encrypted string.


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

//this functions encrypts the string
string encrypt(string s)
    /*ch is a string which contain both uppercase
    and lowercase alphabets beacuse it will be used
    to generate randon alphabets for encryption*/
    string ch = "";

    //Inserting uppercase alphabets in ch
    for (int i = 65; i <= 90; i++)
        ch += char(i);

    //Inserting lowercase alphabets in ch
    for (int i = 97; i <= 122; i++)
        ch += char(i);
    int l = ch.length();

    //Encrypted message will be stored in result
    string result = "";

    //Mixing original string with random alphabets
    for (int i = 0; i < s.length(); i++)
        if (s[i] != ' ')
            for (int j = 0; j < 6; j++)

                /*only second alphabet from each word is valid 
                in result sring while rest of  the alphabets are
                random(for encryption)*/
                if (j == 1)
                    result += s[i];
                    result += ch[rand() % l];
            result += ' ';
    return result;
int main()
    string s;
    cout << "Enter message: ";
    getline(cin, s);
    cout << "Encrypted Message: ";
    cout << encrypt(s);
    return 0;


Enter message: Code Speedy
Encrypted Message: KCeqPP ZoLKcv ydCKlF RexhfC jSlUPX OpgHPs HesnpL 
                   HeqIRy tdYkfy NyzUzP

Each word in the encrypted message contains a letter from the original message.
If we look closely we can see that by taking the second character from each word of the encrypted message, we can get the original message.

Also read: C++ Program to Encrypt and Decrypt a String

Leave a Reply

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