Coders Packet

Simple Message Encryption And Decryption Program in C++

By Deepak Nagar

We will use a key in which we convert the alphabets to different alphabets from the key which corresponds to that alphabets. This program is written in C++.

This is the complete description of the project. We have written this program in C++. First, we include the input and output library (iostream) #includeand modern C++ standard string library (string) #.

Then we use namespace to tell the compiler to looks for functions in the standard library (using namespace std). Then we declare two functions encryption and decryption. We use these two functions to encrypt or decrypt the message.

Now we have the main function in which we use getline function to enter the message or cyphertext. Then we use a do-while loop for valid choices. Inside the do block, we call functions depending upon a user's input. We use two string variables in the encryption function, one for alphabets (plain text ) and another one for Cyphertext.

Now we use for loop to iterate through the message and use the find method in the string to find the location of the character in the alphabet string. Now we replace the alphabet with the corresponding cypher_character in the key string. In the decryption function, we look in the key string and find the location of cypher_character, and then we use it to replace it with a corresponding character in the alphabet string.

Algorithm:-

1)Enter the message or cypher_text.

2)Select the task encryption or decryption.

3)If you choose to encrypt then look into the alphabet string for the matching character and know the location of that character and replace the character with the Cypher_character at the same location in the key string.

Or if you choose to decrypt then look into the key string for the matching Cypher_character and know the location of that Cypher_character and replace the Cypher_character with character at the same location in the alphabet string.

4)print the encrypted or decrypted message.

//Encryption function
void encryption(string message)
{
 string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
 string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
 int index {0};
 for(int i=0;i<message.length();i++)
 {
     if(isalpha(message.at(i)))
     {
         index= alphabet.find(message.at(i));
         message.at(i)=key.at(index);
     }
 }
cout<<"The encrypyted message is :"<<endl;
cout<<message<<endl;
}
//Decryption Function
void decryption(string message)
{
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
 string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};

 int index {0};
 for(int i=0;i<message.length();i++)
 {
     if(isalpha(message.at(i)))
     {
         index= key.find(message.at(i));
         message.at(i)=alphabet.at(index);
     }
 }
 cout<<"The decrypyted message is :"<<endl;
 cout<<message<<endl;
}

 

 

Output or the test cases are in the below images.

program3program4

 

You can download the complete source code provided as a zip file.

 

Download project

Reviews Report

Submitted by Deepak Nagar (Deeip)

Download packets of source code on Coders Packet