Must compile in Visual Studio. A c++ program. Must Show output screen. Explain e
ID: 3661603 • Letter: M
Question
Must compile in Visual Studio. A c++ program. Must Show output screen. Explain each code line and put in comments. For this programming homework to implement encryption/decryption functions for Caesar Cipher, and brute force attack function for cipher texts.
1 Details of the program
You need to do the following tasks:
Implement encryption/decryption functions that take a key (as an integer in 0, 1, 2,
..., 25) and a string. The function should only operate on the characters ‘a’, ‘b’, ..., ‘z’ (both upper and lower case), and it should leave any other characters, unchanged.
Implement a function that performs a brute force attack on a ciphertext, it should print a list of the keys and associated decryptions. It should also take an optional parameter that takes a substring and only prints out potential plaintexts that contain that decryption.
Show the output of your encrypt function on the following (key, plaintext) pairs:
k = 6, plaintext = “Get me a vanilla ice cream, make it a double.”
k = 15, plaintext = “I don’t much care for Leonard Cohen.”
k = 16, plaintext = “I like root beer floats.”
Show the output of your decrypt function on the following (key, ciphertext) pairs:
k = 12, ciphertext = “NDUZS FTQ BUZQ OAZQE.’
k = 3, ciphertext = “FDHVDU QHHGV WR ORVH ZHLJKW.”
k = 20, ciphertext = “UFGIHXM ULY NUMNYS.”
Show the output of your attack function on the following ciphertexts, if an optional keyword is specified, pass that to your attack function:
ciphertext = “GRYY GURZ GB TB GB NZOEBFR PUNCRY.” keyword = “chapel”
ciphertext = “WZIV KYV JYFK NYVE KYV TPDSRCJ TIRJY.” keyword = “cymbal”
ciphertext = “BAEEQ KLWOSJL OSK S ESF OZG CFWO LGG EMUZ.” no keyword
Explanation / Answer
#include <iostream>
using namespace std;
// I didn't understand the last part, optional parameter
// encrypt function
void encrypt(string str, int key){
// make a temorary string same as the actual string
string str1 = str;
// loop through every character of the input string
for(int i = 0; i < str.size(); i++){
// if the character is an alphabet, shift it according to the key
if((str[i] >= 'a' && str[i] <= 'z')){
str1[i] += key;
if(str1[i] > 'z') str1[i] -= 26;
}
else if(str[i] >= 'A' && str[i] <= 'Z'){
str1[i] += key;
if(str1[i] > 'Z') str1[i] -= 26;
}
}
cout << str1 << " ";
}
void decrypt(string str, int key){
// make a temorary string same as the actual string
string str1 = str;
// loop through every character of the input string
for(int i = 0; i < str.size(); i++){
// if the character is an alphabet, shift it according to the key
if((str[i] >= 'a' && str[i] <= 'z')){
str1[i] -= key;
if(str1[i] < 'a') str1[i] += 26;
}
else if(str[i] >= 'A' && str[i] <= 'Z'){
str1[i] -= key;
if(str1[i] < 'A') str1[i] += 26;
}
}
cout << str1 << " ";
}
int main(){
encrypt("Get me a vanilla ice cream, make it a double.", 6);
encrypt("I don’t much care for Leonard Cohen.", 15);
encrypt("I like root beer floats.", 16);
cout << " ";
decrypt("NDUZS FTQ BUZQ OAZQE.", 12);
decrypt("FDHVDU QHHGV WR ORVH ZHLJKW.", 3);
decrypt("UFGIHXM ULY NUMNYS.", 20);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.