Currently I am trying to make a brute force caesar cipher in C++ which shows all the iterations as it guesses each key.
Here is my code thus far:
#include <iostream>
using namespace std;
int main()
{
char message[100], ch;
int i;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
for(int key = 0; key<26 ; key++)
{
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
cout << "Decrypted message: " << message << " " << endl ;
}
return 0;
}
The problem I have is when it is showing the guesses, it seems to skip letters when they are lowercase and just gets stuck when they are uppercase and repeats the same string. In the example the code is LT HDGJWJFLQJX which should decrypt to GO CYBEREAGLES. Both the uppercase and lowercase program outputs are shown in the images attached.