This is the problem:
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:
:mmZ\dxZmx]Zpgy
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.
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- void input(char message[100], int& key);
- vector<char> encrypt(char message[100], int key);
- int main()
- {
- input(message[100], key);
- vector<char> encrypt(char message[100], key);
- return 0;
- }
- void input(char message[100], int& key)
- {
- cout << "Please enter an encrypted message: \n";
- cin.getline(message,100);
- }
- vector<char> encrypt(char message[100], int key)
- {
- int counter;
- char output[100];
- for(key = 1; key <= 100; key++)
- {
- for(counter = 0; message[counter]! = '\0' ; counter++)
- {
- if(message[counter] - key < 32)
- output[counter] = message[counter] - key - 32 + 127;
- else
- output[counter] = message[counter] - key;
- }
- cout << "Decrypted message using Key" << key << " : ";
- cout << output << endl;
- }
- }