Caesar Cipher 99% done

I am almost done with this code. I am just having trouble with one encryption. When I decrypt "TekwbqiQtqci" with a key of 42 it decrypts as "DU[gRaYAdaSY" instead of "DouglasAdams". The encryptions part works fine it is just the decryption part that is causing the problem and it just seems to be on this one word. Any help would be appreciated.

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstring>
using namespace std;

void caesar_encrypt(char src[100], int key, int dist)
{
    for (int i=0; i < dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] + key - 'A' + 26) % 26) + 'A');
        }
        else if (src[i] >= 'a' && src[i] <= 'z')
        {
            src[i] = (char)(((src[i] + key - 'a' + 26) % 26) + 'a');
        }
    }
}

void caesar_decrypt(char src[100], int key, int dist)
{
    for (int i=0; i < dist; i++)
    {
        if (src[i] >= 'A' && src[i] <= 'Z')
        {
            src[i] = (char)(((src[i] - key - 'A' + 26) % 26) + 'A');
        }
        else if (src[i] >= 'a' && src[i] <= 'z')
        {
            src[i] = (char)(((src[i] - key - 'a' + 26) % 26) + 'a');
        }
    }
}

main ()
{
    char caesar[10];
    static const char encrypt[] = "encrypt";
    static const char decrypt[] = "decrypt";
    int key;
    char src[100];
    int result1;
    int result2;
    int dist;



    cout << "Enter operation: encrypt or decrypt" << endl;
    cin >> caesar;
    cout << "Enter key" << endl;
    cin >> key;
    cout << "Enter text to encrypt/decrypt" << endl;
    cin >> src;

    dist = strlen (src);

    result1 = strcmp (caesar, encrypt);
    result2 = strcmp (caesar, decrypt);

    if(result1 == 0)
    {
        caesar_encrypt(src, key, dist);
    }
    if(result2 == 0)
    {
        caesar_decrypt(src, key, dist);
    }

    cout << "Result:" << endl;
    cout << src << endl;
}
It's because the % operator won't make a negative # positive so u'll have to add 26 more if the (..) is negative.

-13 % 26 == -13
13 % 26 == 13

Last edited on
Line 30, lets see what happens if key is 42 and src[i] is 'e' (assuming ASCII is used).

1
2
3
4
5
6
7
src[i] = (char)(((src[i] - key - 'a' + 26) % 26) + 'a');
src[i] = (char)((('e' - 42 - 'a' + 26) % 26) + 'a');
src[i] = (char)(((101 - 42 - 97 + 26) % 26) + 97);
src[i] = (char)((-12 % 26) + 97);
src[i] = (char)(-12 + 97);
src[i] = (char)(85);
src[i] = 'U';


As you see the problem is that operator% does not work as you expect on negative values.
Last edited on
Something is wrong with decrypt. However, if you can encrypt then you can decrypt. Something like this:

1
2
3
4
void caesar_decrypt(char src[100], int key, int dist)
{
    caesar_encrypt(src, 26 - (key % 26), dist);
}


Encrypt a key of 26 is the same as decrypting a key of 0
Last edited on
Topdog2904, thanks for your help. I changed the code and it worked perfectly. Thanks again.
thank you so much this helped me a lot!!
Topic archived. No new replies allowed.