-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (99 loc) · 2.87 KB
/
main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "vigenere.h"
using namespace std;
void print_specification();
int main(int argc, char** argv) {
string inname = "";
string mode = "";
string key = "";
string outname = "";
vector<string> cmd_args(&argv[1], &argv[argc]);
if (argc <= 1) {
print_specification();
return 0;
}
else {
for (int i = 0; i < cmd_args.size(); i++) {
if (cmd_args[i].compare("-h") == 0 || cmd_args[i].compare("--help") == 0) {
print_specification();
return 0;
}
if(cmd_args[i].compare("-k") == 0) {
key = cmd_args[i + 1];
}
else if (cmd_args[i].compare("-i") == 0) {
inname = cmd_args[i + 1];
}
else if (cmd_args[i].compare("-o") == 0) {
outname = cmd_args[i + 1];
}
else if (cmd_args[i].compare("-d") == 0) {
if (mode.compare("") != 0) {
cout << "ERROR: More than one mode specified.\n";
print_specification();
return 0;
}
mode = "d";
}
else if (cmd_args[i].compare("-e") == 0) {
if (mode.compare("") != 0) {
cout << "ERROR: More than one mode specified.\n";
print_specification();
return 0;
}
mode = "e";
}
}
}
if (key.compare("") == 0 || inname.compare("") == 0 || outname.compare("") == 0 || mode.compare("") == 0) {
cout << "ERROR: One or more elements not found.\n";
cout << "Size of args: " << cmd_args.size() << endl;
cout << "Input: " << inname << endl;
cout << "Output: " << outname << endl;
cout << "Key: " << key << endl;
print_specification();
return 0;
}
cout << "Trying to open " << inname << "\n";
ifstream infile;
infile.open(inname);
ofstream outfile(outname);
if (infile.is_open()) {
Vigenere cipher(key);
string line;
if (mode.compare("d") == 0) {
while (getline(infile, line)){
outfile << cipher.decrypt(line);
}
}
else if (mode.compare("e") == 0) {
while (getline(infile, line)) {
outfile << cipher.encrypt(line);
}
}
infile.close();
outfile.close();
string temp = !mode.compare("d") ? "decryption." : "encryption.";
cout << outname << " generated for " << temp << "\n";
}
else cout << "ERROR: Unable to find file : \"" << inname << "\".";
return 0;
}
void print_specification() {
cout << "\nVigenere Cipher." << "\n\n";
cout << "Usage:\n";
cout << "\tvigenere_cipher -d -k <key> -i <plaintext> -o <ciphertext>\n";
cout << "\tvigenere_cipher -e -k <key> -i <ciphertext> -o <plaintext>\n";
cout << "\tvigenere_cipher -h | --help\n";
cout << "\n";
cout << "Options:\n";
cout << "\t-k\t\tSet the key for the cipher.\n";
cout << "\t-i\t\tFile with the contents that want to be decrypted/encrypted.\n";
cout << "\t-o\t\tFile with the contents of the decrypted/encrypted message.\n";
cout << "\t-d\t\tSet decryption mode for the file.\n";
cout << "\t-e\t\tSet encryption mode for the file.\n";
cout << "\t-h --help\tShow this screen.\n\n";
}