-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt.c
54 lines (47 loc) · 1.19 KB
/
crypt.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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define BUF_SIZE_BYTES 128 / 8
#define KEY_SIZE_BYTES 128 / 8
__uint8_t *expand_key(char *key)
{
int key_len = strlen(key);
__uint8_t *new_key = (__uint8_t *)malloc(KEY_SIZE_BYTES);
// repeats the short key to fill the whole length
// and add some randomness to the key
for (int i = 0; i < KEY_SIZE_BYTES; i++)
{
new_key[i] = key[i % key_len] ^ (i * 5 + 1);
}
return new_key;
}
void encrypt(int infile, int outfile, __uint8_t *key)
{
__uint8_t buf[BUF_SIZE_BYTES];
int key_len = strlen(key);
int len;
while ((len = read(infile, buf, BUF_SIZE_BYTES)) > 0)
{
for (int i = 0; i < len; i++)
{
buf[i] = buf[i] ^ key[i % key_len];
}
write(outfile, buf, len);
}
}
void decrypt(int infile, int outfile, __uint8_t *key)
{
__uint8_t buf[BUF_SIZE_BYTES];
int key_len = strlen(key);
int len;
while ((len = read(infile, buf, BUF_SIZE_BYTES)) > 0)
{
for (int i = 0; i < len; i++)
{
buf[i] = buf[i] ^ key[i % key_len];
}
write(outfile, buf, len);
}
}