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:


The message is obviously encrypted using the enemy's secret code. You have just learned that their encryption method is based on the ASCII code. Individual characters in a string are encoded using this system. (ex: the letter "A" is encoded using the number 65.)

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)
EncryptedChar = (OriginalChar + key)

for example, if the enemy uses key = 10 then the message "Hey" would be encrypted as
H = 72
e = 101
y = 121
Encrypted H = (72 + 10) = 82 = R in ASCII
you get "Hey" = "Ro$"

I have to write a program that decrypts the intercepted message. I know that they key used is a number between 1 and 100 and the program has to decode the message using all 100 keys.

I found out the correct key is 88 and it says "Attack At Dawn!" but I have to display all of them anyway. I've attached a printscreen of what the run program looks like.

this is what I have so far, and it may not make much sense because I am really confused:

#include <iostream>
#include <string>
using namespace std;

int main()
    char EncryptedChar[] = ":mmZ\\dxZmx]Zpgy"; 
    int key;
    char OriginalChar;

    if (OriginalChar + key > 126)
       EncryptedChar = 32 + ((OriginalChar + key) - 127);
       EncryptedChar = (OriginalChar + key);
    for (key = 0; key <= 100, key++)
        cout << "Key: " << key << " Decoded Message: " << EncryptedChar;
    return 0;

Recommended Answers

All 4 Replies

It's (/code), not (\code).

You have two problems: one is that you aren't even trying to write a decryption algorithm. You're just trying to write the encryption algorithm. The other is that you're trying to treat a char array as if it were a char. That makes no sense. Decrypting the message would require looking at each individual character in the char array.

right, how would I do that?

I take it you should at this point in time know how char arrays work. You also know how for loops or while loops work.

Put two and two together and figure out how to write this program yourself.

If you find yourself thinking "I don't know how to do this," you need to ask yourself the question: "What knowledge do I lack that I need to gain in order to solve this problem?" That's often a hard question to answer, but if you keep asking this question and keep getting answers, you will solve your problem.

If, during this process, you find something that is confusing or unexpected about C++ programming, or if you're just wondering if C++ has a particular feature, you can of course ask about it here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.