-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (153 loc) · 5.65 KB
/
main.cpp
File metadata and controls
181 lines (153 loc) · 5.65 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
/***********************************************************
main.cpp
Launches the clonefish program.
Processes user input and passes file and key information to
functions contained within cipher.cpp.
************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include "util.h"
#include "cipher.h"
int main(int argc, char** argv)
{
if (argc > 1) // need at least 2 args to operate
{
if (strcmp(argv[1], "-e") == 0 || strcmp(argv[1], "-E") == 0)
{
/////////////////////////////// ENCRYPTION MODE //////////////////////////////////////////
if (argc < 2) // check for file argument
{
cerr << "Please provide a file to be encrypted: " << endl;
exit(1);
}
// open input file
ifstream inFile(argv[2], ios::in | ios::binary | ios::ate);
if(!inFile)
{
cerr << "File \"" << argv[2] << "\" could not be opened." << endl;
exit(1);
}
string outFileName = argv[2];
outFileName.append(".cfe");
// check for existing ciphertext file
if (fileExists(outFileName))
{
cout << "A ciphertext for this file currently exists. " << endl;
char c = {0};
while (true)
{
cout << "Overwrite? (y/n) ";
string res;
getline(cin, res);
c = res[0];
if (c == 'Y' || c == 'y')
break;
else if (c == 'N' || c == 'n')
exit(0);
else
cout << endl;
}
}
// get password, use as key
char pass[MAX_KEY_LEN];
string passbuf;
int len = 0;
while (len < 5 || len > 56)
{
passbuf = getpass("Please enter a password (5 - 56 characters): ", true);
len = passbuf.size();
}
strcpy(pass, passbuf.c_str());
// open output file
cout << "Output file: " << outFileName << endl;
ofstream outFile(outFileName.c_str(), ios::out | ios::binary);
if(!outFile)
cerr << "Error creating output file.";
// encrypt file
cout << "Performing encryption on file \"" << argv[2] << "\"..." << endl;
encryptFile(&inFile, &outFile, pass);
cout << "Done." << endl;
inFile.close();
outFile.close();
}
else if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "-D") == 0)
{
/////////////////////////////// DECRYPTION MODE //////////////////////////////////////////
if (argc < 2)
{
cerr << "Please provide a file to be decrypted." << endl;
exit(1);
}
// open input file
string in(argv[2]);
ifstream inFile(argv[2], ios::in | ios::binary | ios::ate);
if(!inFile)
{
cerr << "File \"" << argv[2] << "\" could not be opened." << endl;
exit(1);
}
// ensure input file is a .cfe
string outFileName = argv[2];
if (outFileName.size() < 5 ||
outFileName.substr(outFileName.size() - 4, 4) != ".cfe")
{
cout << "File provided is not in proper format (*.cfe). Aborting...\n";
exit(1);
}
//
//int pos = outFileName.find(".cfe");
outFileName = outFileName.substr(0,outFileName.size() - 4);
if (fileExists(outFileName))
{
cout << "A file named \"" << outFileName << "\" already exists. ";
char c = {0};
while (true)
{
cout << "Overwrite? (y/n) ";
string res;
getline(cin, res);
c = res[0];
if (c == 'Y' || c == 'y')
break;
else if (c == 'N' || c == 'n')
exit(0);
else
cout << endl;
}
}
// get password, use as key
char pass[MAX_KEY_LEN];
string passbuf;
int len = 0;
while (len < 5 || len > 56)
{
passbuf = getpass("Please enter the password for this file: ", true);
len = passbuf.size();
}
strcpy(pass, passbuf.c_str());
// open output file
cout << "Output file: " << outFileName << endl;
ofstream outFile(outFileName.c_str(), ios::out | ios::binary);
if(!outFile)
cerr << "Error creating output file.";
cout << "Performing decryption on file \"" << argv[2] << "\"..." << endl;
decryptFile(&inFile, &outFile, pass);
cout << "Done." << endl;
inFile.close();
outFile.close();
}
else
{
cerr << "Option not recognized." << endl;
exit(1);
}
}
else
{
cout << "Please specify a valid option.\nUsage is:\n"
"\n\tencrypt: ./clonefish -e (filename)"
"\n\tdecrypt: ./clonefish -d (filename) <-- must be a .cfe file\n\n";
}
return 0;
}