-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrypt.cpp
More file actions
235 lines (214 loc) · 5.73 KB
/
Crypt.cpp
File metadata and controls
235 lines (214 loc) · 5.73 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
//functions
void encrypt(string Plain);
void decrypt(string Cypher, string Key, int Key2);
int letter(char lett);
int sqrrt(float number);
int main()
{
//Ask user for what they want to do
string response;
cout << "Welcome to the Encryption System..." << endl;
cout << "To Encrypt your message, please enter 1..." << endl;
cout << "To Decrypt your message, please enter 2..." << endl;
std::getline(cin, response);
//encryption
if(response == "1")
{
string PlainText;
cout << "Enter your message to encrypt and press enter" << endl;
std::getline(cin, PlainText);
encrypt(PlainText);
return 0;
}
//decryption
else if(response == "2")
{
string CypherText, Key1;
int Key2;
cout << "Enter your encrypted message and press enter" << endl;
std::getline(cin, CypherText);
cout << "Enter key 1 and press enter" << endl;
std::getline(cin, Key1);
cout << "Enter key 2 and press enter" << endl;
cin >> Key2;
decrypt(CypherText, Key1, Key2);
return 0;
}
//invalid response
else
{
cout << "Invalid Response!" << endl;
return 1;
}
}
//encryption function
void encrypt(string Plain)
{
char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //characters to store hex values
int temp6, temp1, temp3, temp4, record[Plain.length()], j = 0, temp7;
float temp2;
string converted = "", rec = "", temp5;
//generate random numbers for 2 respectively
srand(time(NULL));
temp7 = rand() % 1000;
for(int i = 0; i < Plain.length(); i++)
{
//generate random letters for key1
temp6 = rand() % 26;
temp5 = 'a' + temp6;
//convert message to ascii
temp1 = Plain.at(i);
//store position of spaces inputted in key1 add randomly generated letters to it
if(temp1 == 32)
{
record[j] = i - j;
rec = rec + temp5 + to_string(record[j]);
j++;
continue;
}
//math operations on message
temp1 = temp1 * temp1;
temp1 = temp1 - temp7; //subtract randomly generated number from message (key2)
//store encrypted message
while(temp1 > 0)
{
temp2 = temp1 / 16.0;
temp3 = temp2;
temp2 = temp2 - temp3;
temp4 = temp2 * 16;
converted = converted + hex[temp4];
temp1 = temp3;
}
}
//print encrypted message with keys
cout << "Encrypted message: " << converted << endl;
cout << "Key 1: " << rec << endl;
cout << "Key 2: " << temp7 << endl;
}
//decryption system
void decrypt(string Cypher, string Key, int Key2)
{
int temp2 = 16, temp3 = 16 * 16, temp4 = 16 * 16 * 16, temp6[40000], temp7, temp1a[10000], temp8[10000], temp9 = 0, temp11, temp12 = 0;
string de_converted = "", temp5 = "", space = " ", temp15 = "";
char dcrypt[40000];
//Get numbers from key1
for(int i = 1; i < Key.length(); i++)
{
while(isdigit(Key[i]))
{
temp15 = temp15 + Key[i];
i++;
}
temp1a[temp9] = std::stoi(temp15);
temp9++;
temp15 = "";
}
//reverse order of Key1
temp11 = temp9 - 1;
for(int i = 0; i < temp9; i++)
{
temp8[i] = temp1a[temp11];
temp11--;
}
//stores encrypted message in reverse
for(int i = 0; i < Cypher.length(); i++)
{
temp5 = Cypher[i] + temp5;
}
//converts encrypted message in hex to numeric equivalent
for(int i = 1; i <= Cypher.length(); i++)
{
temp6[i - 1] = letter(temp5[i - 1]);
}
//decryption workson a four character system so this converts four hex characters together back to one original text character
int temp13 = Cypher.length() / 4;
for(int i = 0; i < Cypher.length(); i += 4)
{
temp7 = (temp6[i] * temp4) + (temp6[i + 1] * temp3) + (temp6[i + 2] * temp2) + (temp6[i + 3]);
temp7 = temp7 + Key2;
temp7 = sqrrt(temp7);
if(temp13 == temp8[temp12])
{
de_converted = char(temp7) + space + de_converted; //store space if encountered with next letter
temp12++;
}
else
{
de_converted = char(temp7) + de_converted; //store character
}
temp13--;
}
//print plain message
cout << de_converted << endl;
}
//convert hex back to numeric equivalent
int letter(char lett)
{
switch(lett)
{
case '0':
return 0;
break;
case '1':
return 1;
break;
case '2':
return 2;
break;
case '3':
return 3;
break;
case '4':
return 4;
break;
case '5':
return 5;
break;
case '6':
return 6;
break;
case '7':
return 7;
break;
case '8':
return 8;
break;
case '9':
return 9;
break;
case 'A':
return 10;
break;
case 'B':
return 11;
break;
case 'C':
return 12;
break;
case 'D':
return 13;
break;
case 'E':
return 14;
break;
case 'F':
return 15;
break;
default:
break;
}
}
//personal square root function
int sqrrt(float number)
{
float temp1;
for(float m = 1; m * m <= number; m += 0.00001)
{
temp1 = m;
}
return temp1;
}