-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
121 lines (107 loc) · 3.76 KB
/
Copy pathmain.cpp
File metadata and controls
121 lines (107 loc) · 3.76 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
#include "des.h"
#include <cstring>
#include <iostream>
#include <string>
namespace {
void print_usage(const char* program) {
std::cerr
<< "Usage: " << program << " <-e|-d> -k <key> -t <text> [-v]\n"
<< "\n"
<< "Options:\n"
<< " -e Encrypt mode\n"
<< " -d Decrypt mode\n"
<< " -k <key> 16-character hex key (64 bits)\n"
<< " -t <text> 16-character hex input (64 bits)\n"
<< " -v Verbose: show all 16 rounds\n"
<< " -h Show this help\n"
<< "\n"
<< "Examples:\n"
<< " " << program << " -e -k AABB09182736CCDD -t 123456ABCD132536\n"
<< " " << program << " -d -k AABB09182736CCDD -t C0B7A8D05F3A829C\n"
<< " " << program << " -e -k AABB09182736CCDD -t 123456ABCD132536 -v\n";
}
bool is_valid_hex(const std::string& s, std::size_t expected_len) {
if (s.size() != expected_len) return false;
for (char c : s) {
if (!((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'F') ||
(c >= 'a' && c <= 'f'))) {
return false;
}
}
return true;
}
std::string to_upper(std::string s) {
for (char& c : s) {
if (c >= 'a' && c <= 'f') c -= 32;
}
return s;
}
} // namespace
int main(int argc, char* argv[]) {
enum class Mode { none, encrypt, decrypt };
Mode mode = Mode::none;
std::string key;
std::string text;
bool verbose = false;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "-e") == 0) {
mode = Mode::encrypt;
} else if (std::strcmp(argv[i], "-d") == 0) {
mode = Mode::decrypt;
} else if (std::strcmp(argv[i], "-k") == 0 && i + 1 < argc) {
key = argv[++i];
} else if (std::strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
text = argv[++i];
} else if (std::strcmp(argv[i], "-v") == 0) {
verbose = true;
} else if (std::strcmp(argv[i], "-h") == 0 ||
std::strcmp(argv[i], "--help") == 0) {
print_usage(argv[0]);
return 0;
} else {
std::cerr << "Unknown option: " << argv[i] << '\n';
print_usage(argv[0]);
return 1;
}
}
if (mode == Mode::none || key.empty() || text.empty()) {
print_usage(argv[0]);
return 1;
}
key = to_upper(key);
text = to_upper(text);
if (!is_valid_hex(key, 16)) {
std::cerr << "Error: Key must be exactly 16 hex characters.\n";
return 1;
}
if (!is_valid_hex(text, 16)) {
std::cerr << "Error: Input text must be exactly 16 hex characters.\n";
return 1;
}
try {
des::Cipher cipher(key);
std::string result;
if (mode == Mode::encrypt) {
result = cipher.encrypt(text, verbose);
std::cout << "Plaintext : " << des::color::white << text
<< des::color::reset << '\n';
std::cout << "Key : " << des::color::white << key
<< des::color::reset << '\n';
std::cout << "Ciphertext: " << des::color::bold_red << result
<< des::color::reset << '\n';
} else {
result = cipher.decrypt(text, verbose);
std::cout << "Ciphertext: " << des::color::white << text
<< des::color::reset << '\n';
std::cout << "Key : " << des::color::white << key
<< des::color::reset << '\n';
std::cout << "Plaintext : " << des::color::bold_green << result
<< des::color::reset << '\n';
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << '\n';
return 1;
}
return 0;
}