Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignement - Cipher Assignment Specifications Decryption and encryption is a ve

ID: 3564434 • Letter: A

Question

Assignement -Cipher

Assignment Specifications

Decryption and encryption is a very useful activity to hide messages or data. Encryption is utilized to store passwords, store user information and in many other means when the sender does not want the message to be easily readable to anyone other than the intended reader.

Your Assignment

You must write a program that can both decrypt and encrypt a single word that is entered by the user. The initial choice of encryption and decryption is left up to the user. Additionally, the user will enter a value to be utilized when determining how to translate the character.

Alphabetic Positions

Positions start at zero. If the letter is

Explanation / Answer

#include<iostream>
#include<string.h>
using namespace std;

string encryption(string map, string word){ //encrypt function, returns encrypted word
int w;
int len = word.length();
for(int i=0; i<len; i++){
w=(int)word[i];
w-=97;
word[i]=map[w];
}
return word;
}
string decryption(string map, string word){ //decrypt function, returns decrypted word
int w;
int len = word.length();
for(int i=0; i<len; i++){
for(int j=0; j<26;j++){
if(word[i]==map[j]){
int w=j+97;
word[i]=(char)w;
break;
}
}
}
return word;
}

int main(){
string dmap="zyxwvutsrqponmlkjihgfedcba";
string method, map, word;
cout<<"What is the method (encryption or decryption)?"<<endl;
cin>>method;
if(!(method == "encryption" || method=="decryption")){ //method is invalid
cout<<"Error: invalid method choice.";
return 0;
}
else{ //valid method
cout<<"What is the translation map (type 'default' to use default):"<<endl;
cin>>map;
if(map.compare("default")==0){ //default map selected
map=dmap;
}
if(map.length()==26){ //map length is 26
cout<<"What is the single word to translate:"<<endl;
cin>>word;
if(method=="encryption"){ //check encrypt conditions
cout<<"point a"<<endl;
bool etrue=true;
int l=word.length();
for(int i=0; i<l; i++){
if(!islower(word[i])){ //char not in lower case
etrue=false;
break;
}
}
if(etrue)
cout<<encryption(map,word);
else{
cout<<"Entered word not in lower case"<<endl;
return 0;
}   
}
else if(method=="decryption"){ //check decrypt conditions
int l=word.length();
bool isvalid=true;
for(int i=0;i<l;i++){
bool present=false;
for(int j=0;j<26;j++){
if(word[i]==map[j]){
present=true;
break;   
}
}
if(!present){ //charcter in word missing from map
isvalid=false;
break;   
}
}
if(isvalid)
cout<<decryption(map, word);
else{
cout<<"Entered word not in map."<<endl;
return 0;
}
}
}
else{ //map not of length 26
cout<<"Error: map no of legth 26."<<endl;
return 0;
}
  
}
//system("pause"); //used in windows system to pause console output screen
}