You are currently viewing How to Create an Encryption-Decryption project using C++

How to Create an Encryption-Decryption project using C++

Free Coding Ebook 👉 Get Now

How to Create an Encryption-Decryption Project using C++

 Encryption-Decryption project using C++


Hello, coders. Welcome to the codewithrandom blog. Today we will learn how to create an Encryption-Decryption project using C++.

This article will help you to create and understand how the encryption-decryption for a string works.

Encryption-Decryption Project using C++



Human-readable plaintext is transformed into incomprehensible ciphertext during encryption. In essence, this entails transforming legible data into random-looking data. Encryption, to put it simply, is the process of encoding data so that it is concealed from or inaccessible to unauthorized users. Private information, sensitive data, and the security of communication between client apps and servers are all protected by it.


Decryption is the process of restoring data that has been encrypted to its original state. Often, the process of encryption is reversed. It decodes the data such that only an authorized user is able to decrypt it since decryption needs a secret key or password. Decryption is a technique that is used in cyber security that makes it challenging for hackers to intercept and read unauthorized information.

Build Tic Tac Toe Game Code in C++ Using Array

Moving on, let us take a look at what this project does –

This project asks the user for a string. After the user Enters the string, It will show you the option to either encrypt or decrypt the string entered. Then after choosing your desired option, your encrypted or decrypted string will be given as a result.

Overview of the UI:

  • Please enter a string:

After entering the string

  • 1 = Encrypt the string.
  • 2 = Decrypt the string.

Enter the choice. After entering, the result depending upon your choice:

  • Encrypted string:


  • Decrypted string:
#include <iostream>
using namespace std;

int main()
   int i, x;
   char str[100];

   cout << "Please enter a string:\t";
   cin >> str;

   cout << "\nPlease choose following options:\n";
   cout << "1 = Encrypt the string.\n";
   cout << "2 = Decrypt the string.\n";
   cin >> x;

   //using switch case statements
      //first case for encrypting a string
      case 1:
         for(i = 0; (i < 100 && str[i] != '\0'); i++)
            str[i] = str[i] + 2; //the key for encryption is 3 that is added to ASCII value

         cout << "\nEncrypted string: " << str << endl;

      //second case for decrypting a string
      case 2:
         for(i = 0; (i < 100 && str[i] != '\0'); i++)
         str[i] = str[i] - 2; //the key for encryption is 3 that is subtracted to ASCII value

      cout << "\nDecrypted string: " << str << endl;

         cout << "\nInvalid Input !!!\n";
   return 0;

Let us now understand the code:

  • Starting off by writing the header of the code with the required libraries, using namespace std;. We will define the main() function.

The main() function : 

  • Declaring the str variable of type ‘char’ to be encrypted or decrypted with a limit of 100 characters.
  • The program will take the string input from the user.
  • The choice of encrypting or decrypting of the string will be given to you.
  • A switch-case control statement is used to select from the Encryption or Decryption case. If selected other than 1 or 2 options then the console will display “Invalid Input !!!”.

Creating an Employee Database Management project using C++

For Encryption:

A string is encrypted by appending the key-value ‘2’ to the ASCII value of each character in the string.

 Encryption-Decryption project using C++

For Decryption:

The key-value “2” is subtracted from the ASCII value of the characters in order to decrypt a text.

 Encryption-Decryption project using C++


We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with many amazing and fun projects. Learning C++ by creating fun projects makes learning easy and interesting.

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂

Free Coding Ebook 👉 Get Now

Leave a Reply