-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecipher.cpp
More file actions
202 lines (151 loc) · 5.49 KB
/
decipher.cpp
File metadata and controls
202 lines (151 loc) · 5.49 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
//class
class regciphers{
//string ciphers[10]; //cipher messages
char conversionchart[26] = {'a','b','c','d','e','f'
, 'g', 'h','i','j','k','l',
'm','n','o','p','q','r','s',
't','u','v','w','x','y','z'}; //conversion chart
int tries;//user has 3 tries
int i; // for loop for message
int j; //for loop for conversion chart
int s;// for loop for decrypted message
int t;// for loop for decrypted conversion chart
char encrypter;//encrpyter formula
char decrypter; //decryption formula
//ciphers
//string ci1 = ciphers[0] = "stephen was here";
//array of phrases
string ciphers[5] = {"stephen was here","sly fox, try pox, old slots"
,"the quick brown fox jumps over the lazy dog","operation nimrod is a go",
"geronimo geronimo geronimo"}; //ciphers
int randomciphers; //random ciphers
string encryptgemini = " ";//holder for the new encrypted message
string decryptapollo = " "; //holfer for decrypted message
public: //public class
//function that allows different phrases to print out.
void randomizer(){
srand(time(0));
randomciphers = rand()%5; //random ciphers
}
//reg encryption
void encryption(){
//cout << "Reg Message: " << ci1 << endl;
//cout << endl;
//find way to encrypt properly from first phrase to alphapet with caesars cipher PROPERLY
for( i = 0; i < ciphers[randomciphers].length(); i++){ // first statment
for( j =0; j<26; j++){ //caesar converstion
//if the conversionchart and phrase have the same character
if(conversionchart[j] == ciphers[randomciphers][i]){
ciphers[randomciphers][i] = j; // character of sentence is equivalent to conversion chart letter
//formula to encrypt the phrase
encrypter = (j+15)%26;
//conversion chart is equivlanet to encrypter value
if(j = encrypter){
// encrypter number is equal to conversion chat index
encrypter = conversionchart[j];
encryptgemini += encrypter; // converts the number to a string
}
}
}
}
cout <<"Encrypted message: " << encryptgemini << " "; //actual encrypted message
}
//generator actual
//decipher
//attempt to view the entire decrpytion message
void decipher(){
//cout <<"Encrypted message: " << encryptgemini << " "; // encrypted message
for( s = 0; s < encryptgemini.length(); s++){ // first statment
for( t =0; t<26; t++){ //caesar converstion
//if the conversionchart and phrase have the same character
if(conversionchart[t] == encryptgemini[s] ){
encryptgemini[s] = t ; // character of sentence is equivalent to conversion chart letter
//formula to encrypt the phrase
decrypter = (t-15 +26)%26;
//conversion chart is equivlanet to encrypter value
// decrypter number is equal to conversion chat index
decrypter = conversionchart[decrypter];
decryptapollo += decrypter; // converts the number to a string
}
}
}
cout << endl;
//cout <<"decrypted message: " << decryptapollo << " "; //actual encrypted message
}
//message actual
void generatoractual(){
string userchoice;//user option
string userinput;
int tries = 1; // incrementer
int maxamount = 0; // maximum amount
//while loop for game.
while(true){
//user can only place game once before exiting
if(maxamount == 1){
cout << "You have reached the maximum amount of times you can use this game." << endl;
exit(0);
}
cout << "Would you like to play a game?" << endl;
cout << "Type 'yes' to play or 'exit' to quit." << endl;
getline(cin,userinput);
// if user exits the game
if(userinput == "exit"){
cout << "You have exited the game." << endl;
break;
}
//if user wishes to proceed with the game.
if(userinput == "yes"){
cout << "You have chosen to play the game." << endl;
tries = 0;// for each time the user decides the play game, counter resets to 0.
while(tries != 3){
//user input for the game
cout << "Please enter your guess:" << endl;
getline(cin,userchoice); //user input
// if user successflly deciphers correctly.
if(userchoice == decryptapollo){
cout << "You have correctly decrypted the message!" << endl;
exit(0);
//for each time the user gets the decipher wrong, the incrementer goes up. If incrememnter == 3, game over
}else if(!(userchoice == decryptapollo)){
cout << "Incorrect. Please Try Again." << endl;
tries++;
}
// hint appears after 2nd failed attempt. Has first 3 letters, and last 3 letters
if(tries == 2){
cout << "Hint: The Phrase Starts with "<< decryptapollo[1]<< decryptapollo[2]<<decryptapollo[3] << " and ends with "
<< decryptapollo[decryptapollo.length()-3]<< decryptapollo[decryptapollo.length()-2]<< decryptapollo[decryptapollo.length()-1]<< endl;
}
//if the increment equals 3, the loop breaks, and the user didn't decipher the code properly.
if(tries == 3){
cout << "You have failed to decrypt the message." << endl;
cout << "Answer: " << decryptapollo << endl;
exit(0);
maxamount++;
}
}
}
//if user enters invalid input
if(userinput != "yes" || userinput != "exit"){
cout << "Invalid input. Please try again." << endl;
}
}
}
};
// main class
int main(){
//object of the dicpher class. Just testing if the encryption is accurate
regciphers encrypter;
encrypter.randomizer(); // generates random messages
encrypter.encryption(); // encryption algorithm
cout << endl;
encrypter.decipher(); //decipher algorithm
cout << endl;
encrypter.generatoractual(); // object for the actual
return 0;
}