-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
127 lines (117 loc) · 3.73 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
120
121
122
123
124
125
126
127
#include <iostream>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
#include "cipher_tool/cipher.h"
#include "frequential_analysis/kasiski.h"
#include "frequential_analysis/keylength.h"
#define MIN_ARGC 4
#define MAX_ARGC 6
static int requireValidFileDescriptor(const char *path, int flags)
{
int fd = open(path, flags);
if (fd == -1)
{
perror(strerror(errno));
exit(1);
}
return fd;
}
static void requireAKey(int argc)
{
if (argc < MAX_ARGC)
{
std::cout << "No key given! If you don't know the key, you could try a";
std::cout << " frequential analysis attack.\n";
exit(2);
}
}
// Converts the given number to the letter of the latin alphabet corresponding.
static char toLetter(unsigned index)
{
char letters[26] = {
'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'};
return letters[index];
}
// Prints the given vector of keys as a string.
static void printKey(std::vector<unsigned> keys)
{
std::cout << "The key is \"";
for (const auto &key : keys)
std::cout << toLetter(key);
std::cout << "\"" << std::endl;
}
/* argv contains (in the following order):
* - argv[1]:
* - "cipher" to cipher the content of the source file with a key.
* - "unipher" to uncipher the content of the source file with a key.
* - "attack" to uncipher the content of the source file without a key.
* - argv[2] contains the path to the file where to store the result.
* - argv[3] contains the key when needed.
*/
int main(int argc, const char *argv[])
{
if (argc < MIN_ARGC)
{
std::cout << "usage: ./sec <cipher | uncipher | attack>";
std::cout << " <-vigenere | -caesar> <source>";
std::cout << " <destination> [key]\n";
exit(3);
}
const char *action_type = argv[1];
const char *cipher_type = argv[2];
const char *source_path = argv[3];
const char *destination_path = argv[4];
int src = requireValidFileDescriptor(source_path, O_RDWR);
int dest = open(destination_path, O_WRONLY | O_CREAT, 0666);
if (strcmp("attack", action_type) == 0)
{
std::cout << "Attacking " << source_path << "...\n";
if (strcmp("-vigenere", cipher_type) == 0)
{
std::vector<unsigned> keys = findKey(src, dest);
printKey(keys);
unsigned *k = &keys[0];
uncipher(src, dest, k, keys.size());
}
else if (strcmp("-caesar", cipher_type) == 0)
{
caesarFrequentialAnalysisAttack(src, dest);
}
else
{
std::cout << "Unkown type of cipher! Here are your options:";
std::cout << " -vigenere or -caesar.\n";
exit(1);
}
std::cout << destination_path << " contains the unciphered text.\n";
}
else if (strcmp("uncipher", action_type) == 0 || strcmp("cipher", action_type) == 0)
{
requireAKey(argc);
unsigned keys[strlen(argv[5])];
keyToValues(argv[5], keys);
if (strcmp("cipher", argv[1]) == 0)
{
cipher(src, dest, keys, strlen(argv[5]));
std::cout << "Ciphered text of " << source_path << " is in " << destination_path << ".\n";
}
else if (strcmp("uncipher", argv[1]) == 0)
{
uncipher(src, dest, keys, strlen(argv[5]));
std::cout << "Unciphered text of " << source_path << " is in " << destination_path << ".\n";
}
}
else
{
std::cout << "Unkown type of action! Here are your options: cipher";
std::cout << " uncipher or attack.";
exit(1);
}
close(src);
close(dest);
exit(0);
}