Your country is at war and your enemies are using a secretcode to communicate wi
ID: 3644229 • Letter: Y
Question
Your country is at war and your enemies are using a secretcode to communicate with each other. You have managed to intercepta messsage that reads as follows::mmZdxZmx]Zpgy
The message is obviously encrypted using the enemy's secretcode. You have just learned that their encryption method is basedupon the ASCii code. Appendix 3 shows the ASCii 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 andencrypts it as follows:
If(OrigialChar + Key > 126) then
EncryptedChar = 32 + ((OriginalChar +Key) - 127)
Else
EncrptedChar = (OriginalChar Key)
For example, if the enemy uses Key = 10 then the message "Hey"would be encrypted as:
Character ASCii code
--------------------------
H 72
e 101
y 121
Encryped H = (72 + 10) = 82 = R in ASCii
Encrypted e = (101 + 10) = 111 = o in ASCii
Encrypted y = 32 + ((121 + 10) - 127) = 36 = $ in ASCii
Consequently, "Hey" would be transmetted as "Ro$"
Write a program that decrypts the intercepted message. Youonly know that the key used is a number between 1 and 100. Yourprogram should try to decode the message using all possible keysbetween 1 and 100. When you try the valid key, the message willmake sense. For all other keys, the message will appear asgibberish.
Explanation / Answer
#include <iostream>
using namespace std;
void decrypt(char*, const char*, int);
int main(int argc, char * argv[])
{
char *secret = ":mmZdxZmx]Zpgy";
char *output = new char[strlen(secret) + 1];
for (int key = 1; key <= 100; ++key)
{
decrypt(output, secret, key);
cout << "Key " << key << " - " << output << endl;
}
//FOUND at key 7: "Attack at dawn!"
cout << endl;
delete [] output;
return 0;
}
//---------------------------------------------------------
void decrypt(char *output, const char *secret, int key)
{
char *out = output;
for (const char *ptr = secret; *ptr; ptr++, out++)
{
if (*ptr + key > 126)
*out = *ptr + key - 95;
else
*out = *ptr + key;
}
*out = '';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.