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 by 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 30 to 122. Hint: The following formula can be used to encrypt a character C using the Caesar cipher algorithm: E = (C – ‘0’ + k) % 93 + ‘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 Program Please enter your selection: Encrypt Decrypt When 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 whether to continue with encrypting/decrypting messages (i.e. entering letter ‘C’) or exit the program (i.e. entering the letter ‘E’).
IMPORTANT: Can you also put comments on your code? As much as I want to get the right answer, I also want to understand the code, and where I went wrong.
I have written the code which is simple to understand and well commented and to encrypt and decrypt the string using cyper key=7 , i have used the properties of mod operator to have the effect of curcular array. It is advised to see the ASCII table while reffering this code.
CODE:
#include<bits/stdc++.h>
using namespace std;
#define c_key 7 //in question given to use cipher key=7;
string encrypt(string input_str){
string output_str;
for(int i=0;i<input_str.size();i++){
if(input_str[i]>='0' && input_str[i]<='9'){ //if a number
/*since there are 10 numbers so using mod 10 also get the number and add c_key=7 and take mod by 10 this will give you the offset then just add '0';
*/
output_str+=(input_str[i]-'0'+c_key)%10+'0'; //mod 10 used to circularly enrcypt
}
else if(input_str[i]>='A' && input_str[i]<='Z'){ //if uppercase letter
/*since there are 26 uppercase letters so using mod 26 also get the letter and add c_key=7 and take mod by 26 this will give you the offset then just add 'A';
*/
output_str+=(input_str[i]-'A'+c_key)%26+'A';
}
else if(input_str[i]>='a' && input_str[i]<='z'){ //if lowercase letter
/* since there are 26 lowercase letters so using mod 26 also get the letter and add c_key=7 and take mod by 26 this will give you the offset then just add 'a';
*/
output_str+=(input_str[i]-'a'+c_key)%26+'a';
}
}
return output_str;
}
string decrypt(string input_str){
string output_str;
for(int i=0;i<input_str.size();i++){
if(input_str[i]>='0' && input_str[i]<='9'){ //if a number
/*same logic as in encryption in this instead of adding just subtract c_key and add 10(because 10 numbers) because result can be negative when subtracting c_key and then take mod by 10
*/
output_str+=(input_str[i]-'0'-c_key+10)%10+'0';
}
else if(input_str[i]>='A' && input_str[i]<='Z'){ //if uppercase letter
/*same logic as in encryption in this instead of adding just subtract c_key and add 26(because 26 uppercase letter) because result can be negative when subtracting c_key and then take mod by 26
*/
output_str+=(input_str[i]-'A'-c_key+26)%26+'A';
}
else if(input_str[i]>='a' && input_str[i]<='z'){ //if lowercase letter
/*same logic as in encryption in this instead of adding just subtract c_key and add 26(because 26 lowercase letter) because result can be negative when subtracting c_key and then take mod by 26
*/
output_str+=(input_str[i]-'a'-c_key+26)%26+'a';
}
}
return output_str;
}
int main(){
string input_str;
int choice;
char ch='C';
cout<<"Welcome To Cryptographic Techniques\n";
while(ch!='E'){
cout<<"Enter the string\n";
cin>>input_str;
cout<<"Enter 1 to encrypt the above entered string\n";
cout<<"Enter 2 to decrypt the above entered string\n";
cin>>choice;
if(choice==1){
string output_str=encrypt(input_str);
cout<<"Encrypted string is: "<<output_str<<"\n";
}
if(choice==2){
string output_str=decrypt(input_str);
cout<<"Decrypted string is: "<<output_str<<"\n";
}
cout<<"If You want to continue press 'C'\n";
cout<<"If you want to exit press 'E'\n";
cin>>ch;
}
return 0;
}
Input and Output:
Get Answers For Free
Most questions answered within 1 hours.