Question

In: Computer Science

My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...

My question below

Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by ‘c’ and so on and finally ‘z’ is replaced by ‘a’. Thus, if the file contains the following message:

                                    I am a student at csusm

The encrypted message becomes:

                                    J bn b tuvefou bu dtvtn

Then if you get this encrypted message and the key is 1, it means you need to shift the alphabets in the decrypted message by 1 to the left to get the original message back.

This method is called “rot1” indicating that every alphabet rotates by 1 during the encryption. Similarly, rot10 means every alphabet should be rotated by 10 during encryption.

Another method of encryption is to create cryptogram. A cryptogram is a message or word where each letter is replaced with another letter. For example, the string:

                                    The birds name was squawk

might be scrambled to form

                                    xms kbypo zhqs fho obrhfu

Note that space or any punctuation is not scrambled. In this particular case, ‘T’ was replaced with ’x’, each ‘a’ was replaced with ‘h’. etc. We assume there is no difference between lower case or uppercase. Therefore, you must turn all of the letters of a word into lower case before encrypt it. For example, if your work is “McMaster”, the function (word = “tolower(“McMaster”)) makes word to be equal to “mcmaster”.  

Your job is to write a driver program(look at the program guide to see how to create a driver program)that performs different types of encryption or description. First, you need to ask the user to see if the user wants to do encryption or decryption. If the user wants to do encryption, you should find out if the user wants to use “rot” method or cryptogram method. If the user selects the “rot” method, you need to ask for the encryption key. For example, entering 15, means that the user wants to use “rot15” method to do encryption. If the user selects the cryptogram method, you should open the cryptogram text file called “CryptoKey” where the cryptogram string is stored. The cryptogram string should contain a string of 26 letters where the first letter corresponds to letter ‘a’, the second letter corresponds to letter ‘b’ and so on. For example, your cryptogram file may contain the following message.

                                                uhtxvdepqlzfonkcrwyijsgabm

which means ‘u’ corresponds to ‘a’,  ‘h’ corresponds to ‘b’ and … and finally ‘m’ corresponds to ‘z’.

The same procedure should be followed for decryption. You need to ask the user which method the user wants to use (rot or cryptogram) for encryption. Then write the routines that takes an encrypted file and does the decryption to get the original file back.

You are responsible to use string and vector class in this program. For encryption, you need to read the file to be encrypted into a vector of string and place the words (one word per element) into a vector. Then read the elements of your vector one by one and do the encryption and place the encrypted word into another vector. Finally write the content of encrypted vector to an output file.

For decryption, you need to do the same process which is read the file that contains the encrypted information and place the encrypted words one by one into a vector. Then create another vector so that as you decrypt the information you can place the decrypted words into another vector. Finally, the vector that contains the decrypted words (original message) should be written to an output file.

Here is a message you can do your test:

Psychology is the study of mindand behavior[1][2]. It is an academic disciplineand an applied sciencewhich seeks to understand individuals and groups by establishing general principles and researching specific cases [3][4]. In this field, a professional practitioneror researcher is called a psychologistand can be classified as a social, behavioral, or cognitive scientist. Psychologists attempt to understand the role of mental functionsin individual and social behavior, while also exploring the physiologicaland biologicalprocesses that underlie cognitive functions and behaviors. Psychologists explore concepts such as perception, cognition, attention, emotion, intelligence, phenomenology, motivation, brain functioning, personality, behavior, and interpersonal relationships, including psychological resilience, family resilience, and other areas. Psychologists of diverse orientations also consider the unconscious mind. [5]Psychologists employ empirical methodsto infer causaland correlationalrelationships between psychosocial variables. In addition, or in opposition, to employing empiricaland deductivemethods, some especially clinicaland counselingpsychologists at times rely upon symbolic interpretationand other inductivetechniques. Psychology has been described as a "hub science", [6]with psychological findings linking to research and perspectives from the social sciences, natural sciences, medicine, humanities, and philosophy

Place the above message in a text file called “Original.txt” and test your program based on the following:

Execute your program to run the driver routine. Then do the following:

Encrypt the original message using “rot21” (shifting every alphabet to the right 21) and call the file EncryptRot.txt

Encrypt the original message using the following cryptogram and call the file EncryptCrypto.txt

                              yihtqkcdplzuvwmxsgajbrfeon

Decrypt “EncryptRot.txt” file using “rot21” (shifting every alphabet to the left 21) places and call it “DecryptRot.txt”

Decrypt “EncryptCypto.txt” file using the above cryptogram and call it “DecryptCrypto.txt”

Your “DecryptRot.txt” and “DecryptCrypto.txt” should be similar (not exactly identical) to your “Original.txt” file you used for encryption. For example, in the decrypted files all words are in lower case but it is not necessarily the case in the “Original.txt” file.

Need help with finishing this code please:

string getOriginalFileName();

vector <string> PlaceOriginalIntoVector(const string&);

int DriverProgram();

vector <string> EncVectorUsingRot(const vector <string>&);

vector <string> DecVectorUsingRot(const vector <string>&);

vector <string> EncVectorUsingCrypto(const vector <string>&);

vector <string> DecVectorUsingCrypto(const vector <string>&);

string getOriginalFileName()

{

string fileName;

bool done = false;

while (!done)

{

cout << "What is the Name of the original File? ";

cin >> fileName;

ifstream fin;

fin.open(fileName.data());

if (!fin)

cout << "The file name does not exists. Try again: " << endl;

else

{

done = true;

}

}

return fileName;

}

vector <string> PlaceOriginalIntoVector(const string& OriginalFile)

{

vector <string> V;

ifstream fin;

fin.open(OriginalFile.data());

string word;

while (fin >> word)

V.push_back(word);

return V;

}

int DriverProgram()

{bool done = false;

int choice = 0;

while (!done)

{cout << << endl;

cout << "1: Encrypt using \"rot\" method" << endl;

cout << "2: Decrypt using \"rot\" method" << endl;

cout << "3: Encrypt using \"Cryptogram\" method" << endl;

cout << "4: Decrypt using \"Cryptogram\" method" << endl;

cout << "5: Decrypt using \"Cryptogram\" method" << endl;

cout << "Quit the program" << endl;

cout << "Enter your choice: ";

cin >> choice;

if (choice >= 1 && choice <= 5)

done = true;

else

cout << "Try a number between 1 and 5 as shown above" << endl;

}

return choice;

}

vector <string> EncVectorUsingRot(const vector <string>& OriginalVector)

{

cout << "EncVecUsingRot" << endl;

vector <string> V;

return V;

}

vector <string> DecVectorUsingRot(const vector <string>& EncVecRot)

{

cout << "I am in DecVecUsingRot routine " << endl;

vector <string> V;

return V;

}

vector <string> EncVectorUsingCrypto(const vector <string>& OriginalVector)

{

cout << "I am in EncVecUsingCrypto routine" << endl;

vector <string> V;

return V;

}

vector <string> DecVectorUsingCrypto(const vector <string>& EncVecCrypto)

{

cout << "I am in DecVecUsingCrypto routine" << endl;

vector <string> V;

return V;

}

int main()

{

string OriginalFile = getOriginalFileName();

cout << "The name of the file is: " << OriginalFile << endl;

vector <string> OriginalVector = PlaceOriginalIntoVector(OriginalFile);

int last = OriginalVector.size()-1;

cout << "First word in the file is: " << OriginalVector[0] << endl;

cout << "Last word in the file is: " << OriginalVector[last] << endl;

vector <string> EncVecRot, DecVecRot, EncVecCrypto, DecVecCrypto;

bool done = false;

while (!done)

{

int choice = DriverProgram();

if (choice == 1)

{

cout << "choice is 1: EncUsingRot" << endl;

EncVecRot = EncVectorUsingRot(OriginalVector);

}

else if (choice == 2)

{

cout << "choice is 2: DecUsingRot" << endl;

DecVecRot = DecVectorUsingRot(EncVecRot);

}

else if (choice == 3)

{

cout << "choice is 3: EncUsingCrypto" << endl;

EncVecCrypto = EncVectorUsingCrypto(OriginalVector);

}

else if (choice == 4)

{

cout << "choice is 4: DecUsingCrypto" << endl;

DecVecCrypto = DecVectorUsingRot(EncVecCrypto);

}

else if (choice == 5)

{

cout << "choice is 5: Quiting the program" << endl;

done = true;

}

}

system("pause");

return 0;

}

Solutions

Expert Solution

vector <string> EncVectorUsingRot(const vector <string>& OriginalVector)

{

cout << "EncVecUsingRot" << endl;

vector <string> V;

for(int i=0;i<OriginalVector.size();i++)

{

string s=OriginalVector[i];

for(int j=0;j<s.length();j++)

{

if(s[j]>='a' && s[j]<='z)

{

s[j]=s[j]-'a';

s[j]=(s[j]+21)%26;

s[j]=s[j]+'a';

}

else if(s[j]>='A' && s[j] <='Z')

{

s[j]=s[j]-'A';

s[j]=(s[j]+21)%26;

s[j]=s[j]+'A';

}

}

V.push_back(s);
}

return V;

}

vector <string> DecVectorUsingRot(const vector <string>& EncVecRot)

{

cout << "I am in DecVecUsingRot routine " << endl;

vector <string> V;

for(int i=0;i<EncVecRot.size();i++)
{
   string s=EncVecRot[i];
   for(int j=0;j<s.length();j++)
   {
       if(s[j]>='a' && s[j]<='z')
       {
           s[j]=s[j]-'a';
           s[j]=(s[j]-21)%26;
           s[j]=s[j]+'a';
       }
       else if(s[j]>='A' && s[j]<='Z')
       {
           s[j]=s[j]-'A';
           s[j]=(s[j]-21)%26;
           s[j]=s[j]+'A';
       }
   }
   V.push_back(s);
}

vector <string> EncVectorUsingCrypto(const vector <string>& OriginalVector)

{

cout << "I am in EncVecUsingCrypto routine" << endl;

vector <string> V;

string cryptogram="yihtqkcdplzuvwmxsgajbrfeon";

for(int i=0;i<OriginalVector.size();i++)
{
       string s=OriginalVector[i];
       for(int j=0;j<s.length();j++)
       {
           if(s[j]>='a' && s[j]<='z')
           {
               s[j]=s[j]-'a';
               s[j]=cryptogram[s[j]];
           }
           else if(s[j]>='A' && s[j] <='Z')
           {
               s[j]=s[j]-'A';
               s[j]=cryptogram[s[j]];
           }
       }
       V.push_back(s);
}

return V;

}


vector <string> DecVectorUsingCrypto(const vector <string>& EncVecCrypto)

{

cout << "I am in DecVecUsingCrypto routine" << endl;

vector <string> V;

string cryptogram="yihtqkcdplzuvwmxsgajbrfeon";

map<char,char> m;
for(int i=0;i<26;i++)
{
   char c='a'+i;
   m.insert({cryptogram[i],c});
}

for(int i=0;i<EncVecCrypto.size();i++)
{
   string s=EncVecCrypto[i];
   for(int j=0;j<s.length();j++)
   {
       char c=s[j];
       s[j]=m[c];
   }
   V.push_back(s);
}

return V;

}


Related Solutions

write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
We like to apply text encryption and decryption as follows. In text encryption, for each letter...
We like to apply text encryption and decryption as follows. In text encryption, for each letter and numeric character in the plaintext (i.e., a-z, A-Z and 0-9), it is “shifted” a certain number of places down the alphabet or numbers. For example, assuming a shifted key offset of 3 is used, ‘A’ would be substituted by ‘D’, ‘B’ would become ‘E’, and so on. Similarly, ‘0’ would become ‘3’ and so on. Note that wrap-around will be applied at the...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
Write a complete C program that read the text below and save the text in a...
Write a complete C program that read the text below and save the text in a new file "second.txt" with the same text written in all uppercase. "Beedle the Bard was an English wizard and author of wizarding fairytales. Beedle was born in Yorkshire, England. At some point in his life he wrote The Tales of Beedle the Bard . The only known image of Beedle is a woodcut that shows him with a "luxuriant" beard. Beedle wrote in ancient...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch >...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } mean??? I understand that it has something to do with ASCII characters and makes sure that if the encryption/decryption character is more than "z", then it would loop back to "a" instead of outputting a charcter like "{" . I...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT