What's wrong with my Caesar Encoding-Decoding 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
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
72
73
74
75
76
77
#include <iostream>
#include <ctype.h>
#include <conio.h>
using namespace std;

string caesarEncoding(string message,int k)
{

for(int x=0;x<message.length();x++)
{

    if(isdigit(message[x]))
    {
        message[x]=message[x]+k;
        if(message[x]>57)
        {
           message[x]=(message[x]%57)+48;
        }
    }
    else if(isalpha(message[x]))
    {
       message[x]=message[x]+k;
        if(message[x]>90)
     {
         message[x]= (message[x]%90)+65;
     }
    }
    else
        break;


}

return message;
}
void caesarDecoding(string message)
{

for(int k=0;k<26;k++)
{
    for(int x=0;x<message.length();x++)
    {
      if(isdigit(message[x]))
    {
        message[x]=message[x]-k;
        if(message[x]>57)
        {
          message[x]=(message[x]%57)+48;
        }
    }
    else if(isalpha(message[x]))
    {
        message[x]=message[x]-k;
        if(message[x]>90)
     {
         message[x]= (message[x]%90)+65;
     }
    }
    else
        break;
    }
    cout<<k<<"\t"<<message<<endl;
}
}

int main()
{
 int key;
 string text;
   cout<<"Enter text:";
   cin>>text;
   cout<<"Enter encoding key";
   cin>>key;
   cout<<caesarEncoding(text,key)<<endl;
   caesarDecoding(caesarEncoding(text,key));
   return 0;
}


Here is my code and it runs without giving errors. But it doesnt work right.
Caesar Cipher basicly shifts forward alphabet when encoding with key value for example: for key=1 ABC->BCD so when decoding it should apply -1 to BCD and become ABC again.
I want to do this also with numbers as you can see on my code but just doesnt work right. Please help me fix it. Also here is the problem i get:http://imgur.com/a/zaLzZ
Last edited on
I found at your code several errors. I could write here a lot about these, But that would be pretty hard to me due my poor English language capabilities. Nonetheles, here I have a fixed version of your code, so you could look at the differences to your own. Good luck.
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
#include <iostream>
#include <cctype>

std::string caesarEncoding( std::string message, int k)
{
    k %= 26;
    for (std::size_t x = 0; x < message.length(); ++ x)
    {
        if (isdigit(message[x]))
        {
            message[x] += k;
            message[x] = ( message[x] - 48 ) % 10 + 48;
        }
        else if (isalpha(message[x]))
        {
            message[x] = std::toupper(message[x]) + k;
            message[x] = ( message[x] - 65 ) % 26  + 65;
        }
    }
    return message;
}

void caesarDecoding( std::string message)
{
    for (int k = 1; k < 27; ++ k)
    {
        for (std::size_t x = 0; x<message.length(); ++ x)
        {
            if(isdigit(message[x]))
            {
                // avoiding negative numbers
                if (message[x] - 48 - 1 < 0) message[x] += 10;
                
                -- message[x];
                message[x] = ( message[x] - 48 ) % 10 + 48;
            }
            else if(isalpha(message[x]))
            {
                // avoiding negative numbers
                if (message[x] - 65 - 1 < 0) message[x] += 26;
                
                -- message[x];
                message[x] = ( message[x] - 65 ) % 26 + 65;
            }
        }
        std::cout << k%26 << '\t' << message << std::endl;
    }
}

int main()
{
    using namespace std;

    int key;
    string text;
    cout<<"Enter text: ";
    getline(cin, text); // for taking more then one word
    cout<<"Enter encoding key: ";
    cin>>key;
    cout<<"Encrypted: " << caesarEncoding(text,key)<<endl;
    cout << "Decrypted with:\nkey\tmessage\n----------------\n";
    caesarDecoding(caesarEncoding(text,key));
    return 0;
}
Last edited on
Thanks! It works perfect now.

May i ask why did you use decreased message[x] value at the lines 34 and 42 ? I know its for avoiding negative numbers but it just confused me.

or i would be happy if you try to explain the decryption part :)
Last edited on
The -- message[x]; is just the applying of a -1 key to message[x].
At each decryption round we just switch each character to its predecessor. E.g. C will be changed to B and so on. Consider that we applying just a key of -1 at each decrypting round to the same message, so after 26 rounds of -1 we have gotten all decryption cases.

Consider, if you want a single decryption with a concrete key, you could just use the encrypting function with the negative value of the encryption key.
Last edited on
Topic archived. No new replies allowed.