-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa1.cpp
More file actions
executable file
·144 lines (109 loc) · 3.39 KB
/
rsa1.cpp
File metadata and controls
executable file
·144 lines (109 loc) · 3.39 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
#include <stdio.h>
#include <memory.h>
#include <getopt.h>
#include <ctype.h>
#include "generate_keys.h"
#include "encryption.h"
#include "decryption.h"
void help()
{
printf("RSA 1.0 by Vera Myshelova\n");
printf("Options:\n");
printf(" -g Generate keypair\n");
printf(" Arguments:\n");
printf(" -n Key size in bits (for example 1024)\n");
printf(" -r filename Private key file name\n");
printf(" -p filename Public key file name\n");
printf(" -e Encrypt file\n");
printf(" Arguments:\n");
printf(" -i filename Input file name\n");
printf(" -o filename Output file name\n");
printf(" -p filename Public key file name\n");
printf(" -d Decrypt file\n");
printf(" Arguments:\n");
printf(" -i filename Input file name\n");
printf(" -o filename Output file name\n");
printf(" -r filename Private key file name\n");
printf(" -h Show this help\n");
return;
}
int main(int argc, char *argv[])
{
enum {
OPT_GenKeys = 1,
OPT_Encrypt = 2,
OPT_Decrypt = 3,
OPT_Help = 4
};
char infile[1000], outfile[1000], priv_file[1000],publ_file[1000];
int key_size;
int opindex = 0;
char c;
opterr = 0;
if (argc == 1) {
printf("Error: no arguments passed.\n");
help();
return 0;
}
while ((c = getopt(argc, argv, "gedhp:r:n:i:o:")) != -1) {
switch (c) {
case 'g':
opindex = OPT_GenKeys;
break;
case 'e':
opindex = OPT_Encrypt;
break;
case 'd':
opindex = OPT_Decrypt;
break;
case 'h':
opindex = OPT_Help;
break;
case 'i':
strcpy(infile, optarg);
break;
case 'o':
strcpy(outfile,optarg);
break;
case 'r':
strcpy(priv_file,optarg);
break;
case 'p':
strcpy(publ_file,optarg);
break;
case 'n':
key_size = atoi(optarg);
break;
case '?':
if ((optopt == 'i') || (optopt == 'o') || (optopt == 'p')
|| (optopt == 'r') || (optopt == 'n'))
fprintf(stderr, "Option -%c requires an argument\n", optopt);
else if (isprint(c))
fprintf(stderr, "Unknown option `-%c`\n", optopt);
else
fprintf(stderr, "Unknown character `%c`\n", optopt);
return 1;
default:
printf("SUUKA\n");
break;
}
}
switch (opindex) {
case OPT_GenKeys:
generate_keys(key_size, publ_file, priv_file);
break;
case OPT_Encrypt:
encryption(infile, outfile, publ_file);
break;
case OPT_Decrypt:
decryption(infile, outfile, priv_file);
break;
case OPT_Help:
help();
break;
default:
abort();
break;
}
return 0;
}