Question

In: Computer Science

This problem needs to be solved with source code. I need a C++ program that will...

This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared.

1.2. We received the following ciphertext which was encoded with a shift cipher:

xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds.


1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is the cleartext?

Solutions

Expert Solution

#include <bits/stdc++.h>

#include <iostream>

#include <ctype.h>

using namespace std;

int main() {

string ciphertext = "xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds";

// A string that has the alphabet in the decreasing order of frequency.

string alphafreq = "etaoinsrhdlucmfywgpbvkxqjz";

string cleartext;

// freq[] will store the frequency of each alphabetic character in the ciphertext

int freq[26] = {0}, i, j, most_freq, key;

char choice;

cout << "Ciphertext : " << ciphertext << '\n';

// Calculate the frequency of alphabet in the ciphertext

for(i = 0; i < ciphertext.length(); i++)

if(ciphertext[i] >= 97 && ciphertext[i] <= 122)

freq[ciphertext[i] - 97]++;

// Find the most frequent alphabet in the ciphertext

most_freq = 0;

for(i = 1; i < 26; i++)

if(freq[i] >= freq[most_freq])

most_freq = i;

/* Decode the ciphertext by substituting the most common alphabet

* in the ciphertext by the alphabet from alphafreq, one by one

*/

for(j = 0; j < 26; j++) {

key = most_freq + 97 - (int)alphafreq[j];

for(i = 0; i < ciphertext.length(); i++) {

if(ciphertext[i] >= 97 && ciphertext[i] <= 122)

if(ciphertext[i] + key >= 97 && ciphertext[i] + key <= 122)

cleartext.push_back(ciphertext[i] + key);

else if(ciphertext[i] + key > 122)

cleartext.push_back(ciphertext[i] + key - 26);

else

cleartext.push_back(ciphertext[i] + key + 26);

else

cleartext.push_back(' ');

}

/* If the user thinks the code has been decoded, he/she can quit,

* or else, retry with the next key.

*/

cout << "\nTry " << j+1 << ". Using key " << key << '\n';

cout << "Decoded text : " << cleartext << '\n';

cout << "Enter 'n' to try the next key, any other character to end.\n";

cin >> choice;

if(choice != 'n')

break;

cleartext.clear();

}

// If cleartext is empty, user didn't accept the result for any key

if(cleartext.empty())

cout << "Unable to decode.\n";

else

cout << "\nCleartext : "<< cleartext << '\n';

return(0);

}

OUTPUT:


Related Solutions

I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Need the correct line of code for c program to solve  this equation . i is a...
Need the correct line of code for c program to solve  this equation . i is a value that the program all ready calculates from user imput. t i = (x*1000)+(y*500)+(z*250
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
I need to make changes to code following the steps below. The code that needs to...
I need to make changes to code following the steps below. The code that needs to be modified is below the steps. Thank you. 1. Refactor Base Weapon class: a.            Remove the Weapon abstract class and create a new Interface class named WeaponInterface. b.            Add a public method fireWeapon() that returns void and takes no arguments. c.             Add a public method fireWeapon() that returns void and takes a power argument as an integer type. d.            Add a public method activate()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT