-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.cpp
More file actions
278 lines (224 loc) · 6.33 KB
/
crypto.cpp
File metadata and controls
278 lines (224 loc) · 6.33 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
// crypto.cpp
//
// Copyright © 2014 Leo Testard <leo.testard@gmail.com>
// Emeric Fremion <scrimet@hotmail.fr>
//
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2.
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.
#include <cstdint>
#include <cstring>
#include <exception>
#include <iostream>
#include <string>
namespace Crypto
{
class Err : public std::exception
{
std::string msg;
public:
Err(std::string const& m) throw() : msg(m) {}
virtual ~Err() throw() {}
virtual const char *what() const throw()
{
return msg.c_str();
}
};
class SymetricCypher
{
public:
virtual void encrypt(std::string &s) const = 0;
virtual void decrypt(std::string &s) const = 0;
virtual ~SymetricCypher() {}
};
class Caesar : public SymetricCypher
{
char key;
void m_encrypt(std::string &s, char key) const
{
uint8_t shift = key - 'a';
for(size_t i = 0; i < s.size(); ++i)
{
uint8_t ch = s[i];
if(isalpha(ch))
{
ch = tolower(ch) - 'a';
ch = (ch + shift) % 26;
s[i] = ch + 'a';
}
}
}
public:
Caesar(char key) : key(key)
{
key = tolower(key);
if(!isalpha(key))
throw Err("Invalid key");
this->key = key;
}
inline virtual void encrypt(std::string &s) const
{
m_encrypt(s, key);
}
inline virtual void decrypt(std::string &s) const
{
uint8_t shift = key - 'a';
m_encrypt(s, 26 - shift + 'a');
}
};
class Monoalpha : public SymetricCypher
{
std::string key;
std::string rev_key;
void m_encrypt(std::string &s, std::string const& key) const
{
for(auto it = s.begin(); it < s.end(); ++it)
{
if(isalpha(*it))
{
char to_cipher = tolower(*it) - 'a';
*it = key[to_cipher];
}
}
}
public:
Monoalpha(std::string const& key) : key(), rev_key()
{
auto it = key.begin();
// build the reverse key
rev_key.resize(26, '\0');
this->key.reserve(26);
for(int i = 0; i < 26; ++i)
{
if(it == key.end())
throw Err("Key too short");
uint8_t idx = *it - 'a';
if(rev_key[idx] != '\0')
throw Err("Repetition of char");
rev_key[idx] = i + 'a';
this->key.push_back(*it);
++it;
}
if(it < key.end())
throw Err("Key too long");
}
inline virtual void encrypt(std::string &s) const
{
m_encrypt(s, key);
}
inline virtual void decrypt(std::string &s) const
{
m_encrypt(s, rev_key);
}
};
class Vigenere : public SymetricCypher
{
std::string key;
public:
Vigenere(std::string const& k) : key()
{
key.reserve(k.size());
for(size_t i = 0; i < k.size(); ++i)
{
if(!isalpha(k[i]))
throw "invalid key";
key.push_back(tolower(k[i]) - 'a');
}
}
virtual void encrypt(std::string& s) const
{
for(size_t i = 0; i < s.size(); ++i)
{
char ch = s[i];
int idx = i % key.size();
if(isalpha(ch))
{
ch = tolower(ch) - 'a';
ch = (ch + key[idx]) % 26;
s[i] = ch + 'a';
}
}
}
virtual void decrypt(std::string& s) const
{
for(auto it = s.begin(); it < s.end(); ++it)
{
if(isalpha(*it))
{
}
}
}
};
}
void usage(std::string const& argv0)
{
std::cerr << "Usage: " << std::endl;
std::cerr << "\t" << argv0 << " CYPHER KEY [ACTION]" << std::endl;
std::cerr << std::endl;
std::cerr << "Cypher: encryption algorithm (caesar, monoalpha, vigenere)." << std::endl;
std::cerr << "Key: encryption key. Depends on cypher method." << std::endl;
std::cerr << "Action: -e (encrypt, default) or -d (decrypt)." << std::endl;
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[])
{
Crypto::SymetricCypher *c;
std::string line;
std::string txt;
if(argc < 3 || argc > 4)
{
std::cerr << "Wrong number of parameters" << std::endl;
usage(argv[0]);
}
if(!strncmp(argv[1], "caesar", 5))
{
char *k = argv[2];
if(strlen(k) != 1)
std::cerr << "Key too long, using first char" << std::endl;
c = new Crypto::Caesar(k[0]);
}
else if(!strncmp(argv[1], "monoalpha", 9))
{
char *k = argv[2];
if(strlen(k) != 26)
std::cerr << "Bad key" << std::endl;
c = new Crypto::Monoalpha(k);
}
else if(!strncmp(argv[1], "vigenere", 8))
{
char *k = argv[2];
c = new Crypto::Vigenere(k);
}
else
{
std::cerr << "Bad cypher" << std::endl;
usage(argv[0]);
}
try
{
/* read input from stdin */
while (std::getline(std::cin, line))
txt += line;
if(argc == 4 && !strncmp(argv[3], "-d", 2))
c->decrypt(txt);
else
c->encrypt(txt);
std::cout << txt << std::endl;
}
catch(std::exception const& e)
{
std::cerr << e.what() << std::endl;
delete c;
return EXIT_FAILURE;
}
delete c;
return EXIT_SUCCESS;
}