decrpyption problem

Ok, i am aware of the rule of asking for help with homework.

i have a good idea of what i need to do, but the code i have written only prints out 1 letter per line until it quits, never giving me my desired result.

The homework asks us to decrypt a message ":mmZ\dxZmx]Zpgy"

The homework reads as follows: "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."

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<string>

using namespace std;

const char X = 100;

void decrypt(char message, int key);

int main()
{
    char message[X];
    
    cout << "Enter message to be decoded. : ";
    cin >> message;
    cout << endl;

    int key;
   
    for(int i = 0;i < sizeof(message); i++)
    {
        decrypt(message[i], key);
    }

    return 0;
}

void decrypt(char message, int key)
{
    char output;

    for(int key = 1;key <= 100; key ++)
    {
        if(message - key < 32)
        {
            output = (((message - key) + 127) - 32);
        }
        else
        {
            output = (message - key);
        }
    cout << "The decrypted message reads: " << output <<endl;
    }

}


The decrypted message reads: y
The decrypted message reads: x
The decrypted message reads: w
The decrypted message reads: v
The decrypted message reads: u
The decrypted message reads: t
The decrypted message reads: s
The decrypted message reads: r
The decrypted message reads: q
The decrypted message reads: p
The decrypted message reads: o
The decrypted message reads: n
The decrypted message reads: m
The decrypted message reads: l
The decrypted message reads: k
The decrypted message reads: j
The decrypted message reads: i
The decrypted message reads: h
The decrypted message reads: g
Last edited on
How is the string encrypted and decrypted?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char encryptChar(char originalChar, int key)
{
    char encryptChar;

    if (originalChar + key > 126)
    {
        encryptChar = 32 +((originalChar + key) - 127);
    }
    else
    {
        encryptChar = (originalChar + key);
    }
    
    return encryptChar;
}


is how it is encrypted
Last edited on
What can this string ":mmZ\dxZmx]Zpgy" possibly be?
i have read elsewhere that it should read "Attack at Dawn!"
Topic archived. No new replies allowed.