Repository navigation
Expand file tree
/
Copy pathotp.cpp
More file actions
293 lines (272 loc) · 11.2 KB
/
Copy pathotp.cpp
File metadata and controls
293 lines (272 loc) · 11.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
===================================================================================
| Program to implement the One-Time Pad Cryptosystem. This program reads a file |
| containing only lowercase or uppercase letters and spaces (assumed newline at |
| end), encrypts the file using random numbers from rand() seeded with time(NULL) |
| and saves the encrypted text and the random keys generated to separate files. |
| All file names are provided by the user running the program from the command |
| line. |
| |
| Usage |
| otp -<option> <read_from> <write_to> <keys> |
| |
| <option> |
| e : encrypt |
| d : decrypt |
| <read_from> : file to read from (original text or encrypted text |
| <write_to> : file to write data to (encrypted text or decrypted text |
| <keys> : secret random keys to be generated or read |
===================================================================================
| Public Functions |
| // Generates two files: one containing the encrypted message and the other |
| // containing the secret randomly generated keys |
| void Encrypt(char* unencrypted_file, char* output_file, char* secret_file ); |
| |
| // Generates one file containing the decrypted message |
| void Decrypt(char* encrypted_file, char* output_file, char* secret_file ); |
| |
| // Auxiliary function, reads a file to a string |
| string Read(char* file_name); |
| |
| // Auxiliary function, writes a string to a file (overwrites the file) |
| void Write(char* file_name, string message); |
===================================================================================
*/
/* including libraries needed for program */
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
/* number of possible characters */
#define MAP_SIZE 53
using namespace std;
/* table with the mapping for the characters. the first of them is at index 1
and the last one is at index MAX_SIZE */
static const char mapping[] = { '\0', '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', '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', ' ' };
/* generates two files: one containing the encrypted message and the other
containing the secret randomly generated keys */
void Encrypt(char* unencrypted_file, char* output_file, char* secret_file);
/* generates one file containing the decrypted message */
void Decrypt(char* encrypted_file, char* output_file, char* secret_file);
/* auxiliary function, reads a file to a string */
string Read(char* file_name);
/* auxiliary function, writes a string to a file (overwrites the file) */
void Write(char* file_name, string message);
/*
================================================================
| Interprets the user command given from the command line. |
| |
| @param argc number of arguments (given by the system) |
| @param argv value of arguments (given by the system) |
| @return 0 if ends without errors |
================================================================
*/
int main(int argc, char* argv[])
{
/* check correct number of arguments */
if (argc == 5){
/* check for encryption */
if ((argv[1][0] == '-') && (argv[1][1] == 'e'))
{
Encrypt(argv[2], argv[3], argv[4]);
}
/* check for decryption */
else if ((argv[1][0] == '-') && (argv[1][1] == 'd'))
{
Decrypt(argv[2], argv[3], argv[4]);
}
else{
std::cout << "Invalid operation" << endl;
}
}
else{
/* show usage and help if wrong syntax or invalid amount of arguments supplied */
std::cout << "Invalid number of arguments supplied." << endl;
std::cout << "Usage:" << endl;
std::cout << "otp - <option> <read_from> <write_to> <keys>" << endl;
std::cout << "" << endl;
std::cout << "<option>" << endl;
std::cout << "e : encrypt" << endl;
std::cout << "d : decrypt" << endl;
std::cout << "<read_from> : file to read from original text or encrypted text" << endl;
std::cout << "<write_to> : file to write data to encrypted text or decrypted text" << endl;
std::cout << "<keys> : secret random keys to be generated or read" << endl;
}
return 0;
}
/*
===================================================================================
| Encrypts the text in a file. Generates two files containing the encrypted |
| file and the randomly generated keys to decrypt the file. the file is |
| assumed to contain only letters and spaces (newline at end). |
| |
| @param unencrypted_file name of the file to encrypt |
| @param output_file name of the file to overwrite the encrypted text |
| @param secret_file name of the file to overwrite the secret keys |
===================================================================================
*/
void Encrypt(char* unencrypted_file, char* output_file, char* secret_file){
/* read the text from the file */
string unencrypted = Read(unencrypted_file);
string encrypted;
/* array to contain the keys */
int* keys = new int[unencrypted.length()];
/* string representation of the keys (permits the use of standard Write()) */
string secret_keys;
/* check for empty file */
if (!unencrypted.empty()){
/* seed random number generator */
srand(time(NULL));
/* traverse the text to encrypt every character */
for (unsigned int i = 0; i < unencrypted.length(); ++i){
/* compare with mapping */
for (unsigned int j = 1; j <= MAP_SIZE; ++j){
/* match found */
if (unencrypted[i] == mapping[j]){
/* generate random key */
keys[i] = (int)(1 + (rand() % 53));
/* append key to string representation of keys */
ostringstream temp;
temp << keys[i];
secret_keys.append(temp.str());
if (i < unencrypted.length()) secret_keys.append(" ");
/* check wrap condition, append encrypted character */
if (j + keys[i] > 53){
encrypted += mapping[j + keys[i] - 53];
}
else{
encrypted += mapping[j + keys[i]];
}
break;
}
}
}
/* save files */
Write(output_file, encrypted);
Write(secret_file, secret_keys);
}
else{
/* signal for empty file (nothing to encrypt) */
std::cout << "ERROR: Empty file" << endl;
}
}
/*
===================================================================================
| Decrypts the text in a file. Generates one file containing the decrypted text. |
| The file is assumed to contain only letters and spaces (newline at end). |
| |
| @param encrypted_file name of the file to decrypt |
| @param output_file name of the file to overwrite the decrypted text |
| @param secret_file name of the file to read the secret keys |
===================================================================================
*/
void Decrypt(char* encrypted_file, char* output_file, char* secret_file){
/* read encrypted text */
string encrypted = Read(encrypted_file);
string unencrypted;
/* read keys */
string secret_keys = Read(secret_file);
/* convert keys to integers */
vector <int> keys;
stringstream s(secret_keys);
string temp;
while (s >> temp){
int number;
istringstream convert(temp);
convert >> number;
keys.push_back(number);
}
/* check for empty file */
if (!encrypted.empty()){
/* traverse the text to decrypt every character */
for (unsigned int i = 0; i < encrypted.length(); ++i){
/* compare with mapping */
for (int j = 1; j <= MAP_SIZE; ++j){
/* match found */
if (encrypted[i] == mapping[j]){
/* check wrap condition, append decrypted character */
if (j - keys[i] < 1){
unencrypted += mapping[j - keys[i] + 53];
}
else{
unencrypted += mapping[j - keys[i]];
}
break;
}
}
}
/* save file */
Write(output_file, unencrypted);
}
else{
/* signal for empty file (nothing to decrypt) */
std::cout << "ERROR: The file specified is empty." << endl;
}
}
/*
===========================================================
| Reads an entire file to a string. |
| |
| @param file_name name of the file to read |
| @return string text read from file |
===========================================================
*/
string Read(char* file_name){
/* file object */
std::ifstream file_stream(file_name, std::ifstream::in);
std::string message;
/* check for successful opening */
if (file_stream.is_open())
{
std::cout << "Attempting to read from file \"" << file_name << "\"...";
/* read file character by character */
while (!file_stream.eof()) message.push_back(file_stream.get());
/* signal success */
std::cout << " Done!" << endl;
/* close file */
file_stream.close();
}
/* signal error opening the file */
else cout << "ERROR: Unable to open file specified, please check the path." << endl;
return message;
}
/*
================================================================
| Overwrites a string to a file. |
| |
| @param file_name name of the file to overwrite |
| @param message string to be written |
| @return string text read from file |
================================================================
*/
void Write(char* file_name, string message){
/* file object */
ofstream file_stream;
file_stream.open(file_name, fstream::out);
/* Check for opened file */
if (file_stream.is_open())
{
std::cout << "Attempting to save to file \"" << file_name << "\"...";
/* write text/keys */
file_stream << message << endl;
/* close file */
file_stream.close();
/* signal success */
std::cout << " Done!" << endl;
}
else
{
/* signal error in opening */
cout << "Error: Could not save the file." << endl;
}
}