-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2.c
266 lines (215 loc) · 5.18 KB
/
cs2.c
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
/**
* Copyright (c) 2015 Jacob Martin
*
* CipherSaber-2 file encryption/decryption, designed to be ran from the
* command line with a key, source file path, and destination file path.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ROUNDS 20
// See function implementations for clarifying comments.
int checkCommand(int argc, char ** argv);
void initState(unsigned char * state, unsigned char * key, int keyLength);
void cipher(unsigned char * state, unsigned char * data, int length);
void decrypt(unsigned char * state, char * key,
char * srcPath, char * destPath);
void encrypt(unsigned char * state, char * key,
char * srcPath, char * destPath);
unsigned char * fileToBytes(char * path, int * stringLength);
int stringToFile(char * string, char * path);
int bytesToFile(unsigned char * bytes, int length, char * path);
int main(int argc, char ** argv)
{
if ( ! checkCommand(argc, argv)) {
printf("Usage:\n");
printf(" cs2 encrypt <key> <source path> <dest path>\n");
printf(" cs2 decrypt <key> <source path> <dest path>\n");
return 0;
}
unsigned char * state;
char * key;
state = malloc(256);
key = malloc(256);
// Grab the key from the argument.
strcpy(key, argv[2]);
if (strcmp(argv[1], "decrypt") == 0) {
decrypt(state, key, argv[3], argv[4]);
}
else {
encrypt(state, key, argv[3], argv[4]);
}
free(state);
free(key);
return 0;
}
/**
* Check the formatting of the command call.
*/
int checkCommand(int argc, char ** argv)
{
if (argc != 5) {
return 0;
}
if (strcmp(argv[1], "encrypt") != 0 &&
strcmp(argv[1], "decrypt") != 0) {
return 0;
}
if (strlen(argv[2]) > 246) {
printf("That key is too long.\n");
return 0;
}
return 1;
}
/**
* Initialize the state array.
*/
void initState(unsigned char * state, unsigned char * key, int keyLength)
{
unsigned char c;
int i, j, n;
for (i = 0; i < 256; ++i) {
state[i] = i;
}
j = 0;
for (n = 0; n < ROUNDS; ++n) {
for (i = 0; i < 256; ++i) {
j += state[i] + key[i % keyLength];
j %= 256;
// Swap the ith and jth elements.
c = state[i];
state[i] = state[j];
state[j] = c;
}
}
}
/**
* Cipher some data.
*/
void cipher(unsigned char * state, unsigned char * data, int length)
{
int i, j, k, n;
unsigned char c;
i = j = 0;
for (n = 0; n < length; ++n) {
i = (i + 1) % 256;
j = (j + state[i]) % 256;
// Swap the ith and jth elements.
c = state[i];
state[i] = state[j];
state[j] = c;
k = (state[i] + state[j]) % 256;
data[n] ^= state[k];
}
}
/**
* Decrypt a file.
*/
void decrypt(unsigned char * state, char * key,
char * srcPath, char * destPath)
{
unsigned char * bytes;
int keyLength;
int dataLength;
bytes = fileToBytes(srcPath, &dataLength);
if ( ! bytes) return;
keyLength = strlen(key);
// Append the IV from the file to the key.
memcpy(key + keyLength, bytes, 10);
initState(state, (unsigned char *) key, keyLength + 10);
cipher(state, bytes + 10, dataLength - 10);
// Write the decrypted string to the destination file.
stringToFile((char *) bytes + 10, destPath);
free(bytes);
}
/**
* Encrypt a file.
*/
void encrypt(unsigned char * state, char * key,
char * srcPath, char * destPath)
{
unsigned char * bytes;
unsigned char * bytesPrepended;
unsigned char random[10];
int keyLength;
int dataLength;
FILE * randomFile;
bytes = fileToBytes(srcPath, &dataLength);
if ( ! bytes) return;
keyLength = strlen(key);
randomFile = fopen("/dev/urandom", "r");
if ( ! randomFile) {
printf("Could not access /dev/urandom.\n");
return;
}
fread(&random, 1, 10, randomFile);
fclose(randomFile);
// Append the IV from random to the key.
memcpy(key + keyLength, random, 10);
initState(state, (unsigned char *) key, keyLength + 10);
cipher(state, bytes, dataLength);
// Prepend random to the encrypted bytes.
bytesPrepended = malloc(dataLength + 10);
memcpy(bytesPrepended, random, 10);
memcpy(bytesPrepended + 10, bytes, dataLength);
// Write the prepended, encrypted bytes to the destination file.
bytesToFile(bytesPrepended, dataLength + 10, destPath);
free(bytes);
free(bytesPrepended);
}
/**
* Read the contents of a file into a cstring.
*/
unsigned char * fileToBytes(char * path, int * stringLength)
{
FILE * file;
unsigned char * data;
file = fopen(path, "r");
if ( ! file) {
printf("Could not open file: %s\n", path);
return 0;
}
fseek(file, 0L, SEEK_END);
*stringLength = ftell(file);
rewind(file);
data = malloc(*stringLength + 1);
if ( ! data) {
printf("Could not allocate enough memory.\n");
return 0;
}
// Null terminate the string.
data[*stringLength] = 0;
fread(data, *stringLength, 1, file);
fclose(file);
return data;
}
/**
* Write the contents of a cstring to a file.
*/
int stringToFile(char * string, char * path)
{
FILE * file;
file = fopen(path, "w");
if ( ! file) {
printf("Could not create/open file: %s\n", path);
return 0;
}
fputs(string, file);
fclose(file);
return 1;
}
/**
* Write the provided bytes to a file.
*/
int bytesToFile(unsigned char * bytes, int length, char * path)
{
FILE * file;
file = fopen(path, "wb");
if ( ! file) {
printf("Could not create/open file: %s\n", path);
return 0;
}
fwrite(bytes, 1, length, file);
fclose(file);
return 1;
}