Caesar Decipher

closed account (NAqL3TCk)
Hi Im working on a Caesar cipher program that is supposed to decipher encrypted messages and decipher them using all keys. I have the decipher function but I was just wondering if there was another way I could do it. Any ideas would be greatly appreciated. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Decipher(char cip[], char key){

  for(int i = 0; cip[i] != '\0'; i++){
      if(cip[i] == ' '){
          cip[i]= cip[i];

      }else if(int(cip[i] >= int(key){
          cip[i] = char(int(cip[i]) - ((int(key) - 65) % 26));

      }else{
            cip[i] = char(26 + int(cip[i]) - ((int(key)-65) % 26);
      }

  }

}
Last edited on
closed account (48T7M4Gy)
deciphering is the inverse of encryption. So what is your encryption function?
closed account (48T7M4Gy)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string.h>

using std::string;
using std::cout;
using std::cin;
using std::endl;

// Note: Only need encrypt (strP, A, E), decrypt is just encrypt(strE, E, A)
string encrypt(const string&, const string&, const string&);
string decrypt(const string&, const string&, const string&);
char solve(string& ctext, string& alphabet, string crib );

string makeCipher(char&, string&);

int main()
{
    char key;
    cout << "    Please enter key: ";
    cin >> key;
    
    const string plain = "THIS IS  A TEST MESSAGE 78";
    cout << "          Plain text: " << plain << endl;

    string A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";// Note space
    
    string E = makeCipher( key, A );
    
    string encryptedMsg = encrypt( plain, A, E) ;
    cout << "   Encrypted message: " << encryptedMsg << endl;
    
    string decryptedMsg = decrypt( encryptedMsg, A, E );
    cout << "   Decrypted message: " << decryptedMsg << endl;
    
    char foundKey = solve(encryptedMsg, A, "IS");
    cout << "         Solution is: " << foundKey << endl;
    
    return 0;
}

string encrypt( const string& plain, const string& A, const string& E )
{
    size_t pos = 0;
    string strEncrypted = "";
    
    for(int i = 0; i < plain.length(); i++)
    {
        pos = A.find( plain[i] );
        strEncrypted += E[pos];
    }
    return strEncrypted;
}

string decrypt( const string& ciphertext, const string& A, const string& E )
{
    return encrypt(ciphertext, E, A);
}

string makeCipher(char& key, string& alphabet)
{
    string strCipher = "";
    size_t limit = alphabet.length();
    size_t base;
    size_t pos = alphabet.find(key);
    
    for(int i = 0; i < alphabet.length(); i++)
    {
        base = (limit + i + pos) % limit;
        strCipher += alphabet[ base ];
    }
    
    return strCipher;
}

char solve(string& ctext, string& alphabet, string crib )
{
    char key = '*';
    size_t limit = alphabet.length();
    size_t found;
    string cipher, trial;
    
    for(int i = 0; i < limit; i++ )
    {
        key = alphabet[i];
        cipher = makeCipher(key, alphabet);
        trial = decrypt(ctext, alphabet, cipher);
        
        found = trial.find(crib);
        if (found != string::npos)
            break;
        else
            key = '*';
    }
    
    return key;
}
Topic archived. No new replies allowed.