-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
88 lines (78 loc) · 1.53 KB
/
main.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
void encrypt(char *key, char *text, size_t size);
void decrypt(char *key, char *text, size_t size);
void print_value(char *value, size_t size)
{
size_t index;
index = 0;
while (index < size)
{
printf("%02x", (unsigned char)value[index]);
index += 1;
}
printf("\n");
}
void generate_key(char *key)
{
unsigned int first;
unsigned int second;
unsigned int third;
unsigned int fourth;
first = rand();
second = rand();
third = rand();
fourth = rand();
memcpy(key, &first, sizeof(int));
memcpy(key + 4, &second, sizeof(int));
memcpy(key + 8, &second, sizeof(int));
memcpy(key + 12, &second, sizeof(int));
return ;
}
char *get_value(char *str, size_t *len)
{
char *rslt;
size_t old_len;
*len = strlen(str);
old_len = *len;
*len = (*len + 16 - 1) / 16 * 16;
rslt = malloc(*len);
if (!rslt)
return NULL;
bzero(rslt, *len);
memcpy(rslt, str, old_len);
return rslt;
}
int main(int argc, char **argv)
{
char key[16];
char *value;
size_t len;
srand(time(NULL));
if (argc < 2)
{
dprintf(2, "Usage: %s [data to encrypt]\n", argv[0]);
return (1);
}
value = get_value(argv[1], &len);
if (!value)
{
dprintf(2, "Malloc error\n");
return (1);
}
generate_key(key);
printf("Key : ");
print_value(key, 16);
printf("value : ");
print_value(value, len);
encrypt(key, value, len);
printf("encrypted : ");
print_value(value, len);
decrypt(key, value, len);
printf("decrypted : ");
print_value(value, len);
free(value);
return (0);
}