Your country is at war and your enemies are using a secret code to communicate w
ID: 3633588 • Letter: Y
Question
Your country is at war and your enemies are using a secret code to communicate with each other. You have managed to intercept a message that reads as follows:
:mmZdxZmx]Zpgy
The message is obviously encrypted using the enemy's secret code. You have just learned that their encryption method is based upon the ASCII code. Appendix 3 of your book shows the ACII character set. Individual characters in a string are encoded using this system. For example, the letter "A" is encoded using the number 65 and "B" is encoded using the number 66.
Your enemy's secret code takes each letter of the message and encrypts it as follows:
If(OriginalChar + Key > 126) then EncryptedChar = 32 + ((OriginalChar + Key ) - 127)
else EncryptedChar = (OriginalChar + Key)
For example, if the enemy used Key = 10 then the message "Hey" would be encrypted as:
Character
ASCII Code
H
72
e
101
y
121
Encrypted H = (72 + 10) = 82 = R in ASCII
Encrypted e = (101 + 10) = 111 = o in ASCII
Encrypted y = (121 + 10) - 127 = 36 = $ in ASCII
Consequently, "Hey" would be transmitted as "Ro$".
Write a program that decrypts the intercepted message. You only know that the key used is a number between 1 and 100. Your program should try to decode the message using all possible keys between 1 and 100. When you try the valid key, the message will make sense. For all other keys the message will appear as gibberish.
Use C++-Strings for this assignment.
Character
ASCII Code
H
72
e
101
y
121
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char * argv[])
{
string decryptedString(const string& secret, int key);
string secretMsg = ":mmZ\dxZmx]Zpgy";
for (int key = 1; key < 101; ++key)
{
cout << "Key " << key << " - "
<< decryptedString(secretMsg, key)
<< endl;
}
//if none were decrypted sucessfully or no decrypted string have meaning, update the filter
//FOUND @key88: "Attack at dawn!"
cout << endl;
cin.get();
return 0;
}
//---------------------------------------------------------
bool filter(const char &chr)
{
return (toupper(chr) >= 'A' && toupper(chr) <= 'Z') || //a-z A-Z
(chr >= '0' && chr <= '9') || //0-9
(chr == ' ' || chr == ' ' || chr == ' ') || //white spaces
(chr == ',' || chr == '.' || //punctuations
chr == ':' || chr == ';' ||
chr == ''' || chr == '"' ||
chr == '?' || chr == '!');
}
//---------------------------------------------------------
string decryptedString(const string& secret, int key)
{
string decoder = "";
for (int i = 0; i < secret.length(); i++)
{
if (filter(secret[i] - key))
decoder += secret[i] - key;
else if (filter(secret[i] - key + 127 - 32))
decoder += secret[i] - key + 127 - 32;
}
if (decoder.length() == secret.length()) //All encrypted chars must be decrypted
return decoder;
return decoder = "";
}
//------------------------OUTPUT-----------------------
Key 1 -
Key 2 -
Key 3 - 7jjWYauWjuZWmdv
Key 4 -
Key 5 -
Key 6 -
Key 7 -
Key 8 -
Key 9 -
Key 10 -
Key 11 -
Key 12 -
Key 13 -
Key 14 -
Key 15 -
Key 16 -
Key 17 -
Key 18 -
Key 19 -
Key 20 -
Key 21 -
Key 22 -
Key 23 -
Key 24 -
Key 25 -
Key 26 -
Key 27 -
Key 28 -
Key 29 -
Key 30 -
Key 31 -
Key 32 -
Key 33 -
Key 34 - wKK8:BV8KV;8NEW
Key 35 - vJJ79AU7JU:7MDV
Key 36 -
Key 37 - tHH57?S5HS85KBT
Key 38 -
Key 39 -
Key 40 -
Key 41 -
Key 42 -
Key 43 -
Key 44 - mAA.08L.AL1.D;M
Key 45 -
Key 46 -
Key 47 -
Key 48 -
Key 49 -
Key 50 -
Key 51 -
Key 52 -
Key 53 -
Key 54 -
Key 55 -
Key 56 -
Key 57 -
Key 58 -
Key 59 -
Key 60 -
Key 61 -
Key 62 -
Key 63 -
Key 64 -
Key 65 -
Key 66 -
Key 67 -
Key 68 -
Key 69 -
Key 70 -
Key 71 -
Key 72 -
Key 73 -
Key 74 -
Key 75 -
Key 76 -
Key 77 -
Key 78 -
Key 79 -
Key 80 -
Key 81 -
Key 82 -
Key 83 -
Key 84 -
Key 85 -
Key 86 -
Key 87 - Buubdl!bu!ebxo"
Key 88 - Attack at dawn!
Key 89 -
Key 90 -
Key 91 -
Key 92 -
Key 93 -
Key 94 -
Key 95 -
Key 96 -
Key 97 -
Key 98 - 7jjWYauWjuZWmdv
Key 99 -
Key 100 -
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.