In: Computer Science
USE C++: Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter with a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if the cipher key is 3, the clear text“HELLO THERE” becomes “KHOOR WKHUH”.Here, we restrict ourselves to encrypting/decrypting digits (0...9), lowercase, and uppercase alphabetic characters. As you know from the ASCII table, the set of these characters correspond to the integers 48to 122.Hint: The following formula can be used to encrypt a character C using the Caesar cipher algorithm: E = (C –‘0’ + k) % 75+ ‘0’ [where k is the cipher key]You need to figure out the formula for decrypting text on your own.NOTE: Use cipher key = 7.Write a C++ program for encrypting and decrypting a given string. Since this program performs two different functionalities (encryption and decryption), prompt the user to select the type of cryptographic technique as shown below: Welcome to Cryptographic Techniques ProgramPlease enter your selection:1. Encrypt2. DecryptWhen the user selects 1 or 2, s/he will be asked to specify an input and output message. Here is an example: Assume that the user enters the following message: HOW ARE YOU DOING? If the user selects to encrypt the message (i.e. option 1), the program will encrypt the original message and outputs an encrypted text. If the user selects to decrypt a message (i.e. option 2), the program will decrypt the encrypted text and outputs the decrypted text on the screen. The program should give the user the option of whether to continue with encrypting/decrypting messages (i.e. entering the letter ‘C’) or exit the program (i.e. entering the letter‘E’).
Program:
#include<string.h>
#include "stdio.h"
#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
//Note: we are using gets hence used char array in the program wherever needed
//function that will be converting the plain text to the cipher text
//default value of k is set to be 7
void encryption(int k=7){
//will hold the plain text, user would enter
char plain_text[100];
//will hold the converted cipher text
string cipher_text;
//will hold ascii value for each character
int c;
//will hold the converted ascii value
int e;
//asking user to enter plain text
cout<<"\n\nEnter the plain text: ";
gets(plain_text);
//traversing in the plain_text and converting to cipher text
for (int i=0; plain_text[i] != '\0'; i++){
c = int(plain_text[i]);
e = (c - '0' + k )%75 + '0';
cipher_text += char(e);
}
//printing the converted cipher text
cout<<"\nCipher text is: "<<cipher_text;
}
//function that will be converting the cipher text to the plain text
//default value of k is set to be 7
void decryption(int k=7){
//will hold the cipher text, user would enter
char cipher_text[100];
//will hold the converted plain text
string plain_text;
//will hold ascii value for each character
int c;
//will hold the converted ascii value
int e;
//asking user to enter plain text
cout<<"\n\nEnter the cipher text: ";
gets(cipher_text);
//traversing in the cipher_text and converting to plain text
for (int i=0; cipher_text[i] != '\0'; i++){
c = int(cipher_text[i]);
e = (c - '0' - k )%75 + '0'; //notice '-' in the formula
if (c>=48 && c<=(48+k-1))
e = (c - '0' - k )%75 + 75 + '0'; // because of negative value after mod
plain_text += char(e);
}
//printing the converted cipher text
cout<<"\nCipher text is: "<<plain_text;
}
int main(){
char select[2] = "C"; //"C" means user will continue entering values
int selection; //will hold user selected encryption or decryption
//welcome message
cout<<"\nWelcome to Cryptographic Techniques Program";
//code run until user is entering 'C'
while (1){
//asking user's choice
cout<<"\n\nPlease enter your selection: ";
cout<<"\n1. Encrypt";
cout<<"\n2. Decrypt\n";
cin>>selection;
cin.ignore(); //^^this is necessary for handling buffer
//based upon the user selection, we'll perform the encryption or decryption
switch(selection){
case 1: encryption();
break;
case 2: decryption();
break;
default: cout<<"\nYou have entered the wrong choice!!";
break;
}
//asking the user to continue or exit
cout<<"\n\nWant to continue encrypting/decrypting?";
cout<<"\nPress 'C' to continue and 'E' to exit\n";
cin>>select;
if(strcmpi(select, "C") != 0)
exit(0);
}
return 0;
}
Output:
1.
2.