int key)
{
int i;
string decoder = “”””;
for (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 = “”””;
}
- 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 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 and encrypts it as follows:
If (OriginalChar + Key > 126) then
EncryptedChar = 32 + ((OriginalChar + Key) – 127)
Else
EncryptedChar = (OriginalChar + Key)
For example, if the enemy uses Key = 10 then the message “Hey” would be encrypted as:
Character ASCII code
H is 72, e is 101, and y is 121.
Encrypted 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 transmitted as “Ro$.”
Write a program that decrypts the intercepted message. The ASCII codes for the unencrypted message are limited to the visible ASCII characters. 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. Have a way for the user to choose which attempt looks correct so that the program will then output the key.
Current code:
/**************************************
Headers
**************************************/
#include
/**************************************
Function Declarations – Message Decryption
**************************************/
//used to filter the characters
bool filter(const char& chr);
//used to descrypt the message
string decryptedString(const string& secret, int key);
int main
{
cout << “**********************************************************************” << endl;
//use a new line to separate the border line above from the program name below
cout << “* *” << endl;
//describe what the program does
cout << “* ‘Message Decoder’ *” << endl;
cout << “* This program will decode from a secret string of characters *” << endl;
cout << “* the enemy is using to send messages back and forth. *” << endl;
cout << “**********************************************************************” << endl;
cin.ignore();
string decryptedString(const string & secret, int key);
string secretMsg = “:mmZ\dxZmx]Zpgy”;
cout << “nThe original undecoded message was: ” << secretMsg;
int key;
for (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”
cout << endl;
cin.get();
}
/**************************************
Function Definitions- Boolean Filter
**************************************/
bool filter(const char& chr)
{
return (toupper(chr) >= ‘A’ && toupper(chr) <= ‘Z’) || //a-z A-Z
(chr >= ‘0’ && chr <= ‘9’) || //0-9
(chr == ‘ ‘ || chr == ‘t’ || chr == ‘n’) || //white spaces
(chr == ‘,’ || chr == ‘.’ || //punctuations
chr == ‘:’ || chr == ‘;’ ||
chr == ”’ || chr == ‘”‘ ||
chr == ‘?’ || chr == ‘!’);
}
/**************************************
Function Definitions- Decryption
**************************************/
string decryptedString(const string& secret
{
int i;
string decoder = “”””;
for (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 = “”””;
}