-
Notifications
You must be signed in to change notification settings - Fork 17
/
setkey.c
117 lines (89 loc) · 2.22 KB
/
setkey.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
/*
* Copyright (C) 2014 Qian Shanhai ([email protected])
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int init_rand()
{
unsigned int now = (unsigned int)time(NULL);
srand(now ^ (unsigned int)getpid());
return 0;
}
#define RC ((rand() % (127 - 29)) + 29)
struct set_key {
unsigned int rand;
char buf[64];
};
int _cmp_key(const void *a, const void *b)
{
struct set_key *pa = (struct set_key *)a;
struct set_key *pb = (struct set_key *)b;
return pa->rand > pb->rand ? 1 : -1;
}
int main(int argc, char *argv[])
{
int i, j, len, total;
char *p, buf[128], tmp[128];
FILE *fp;
struct set_key key[68];
init_rand();
unlink("passwd.c");
p = getpass("server key: ");
strcpy(buf, p);
memset(p, 0, strlen(p));
if (strlen(buf) < 6) {
printf("strlen of key must great than 6 !\n");
return 1;
}
p = getpass("server key again: ");
strcpy(tmp, p);
memset(p, 0, strlen(p));
if (strcmp(buf, tmp) != 0) {
printf("key not match!\n");
return 1;
}
if ((fp = fopen("passwd.c", "w+")) == NULL) {
printf("write passwd.c error\n");
return 1;
}
len = strlen(buf);
fprintf(fp, "#include <string.h>\n");
for (i = 0; i < len; i++) {
total = rand() % 7 + 1;
for (j = 0; j < total; j++) {
fprintf(fp, "\tunsigned char K%u = %d; \n", rand(), RC);
}
fprintf(fp, "\tchar T%d = %d; \n", i, buf[i]);
total = rand() % 5 + 3;
for (j = 0; j < total; j++) {
fprintf(fp, "\tunsigned char K%u = %d; \n", rand(), RC);
}
fprintf(fp, "\tint I%d = %d; \n", i, i);
}
for (i = 0; i < len; i++) {
fprintf(fp, "\tchar *F%d(int i){return i == 0 ? &T%d : &T%d; } \n",
i, i, i == len - 1 ? 0 : i + 1);
}
fprintf(fp, "char fall()\n{\n\treturn ");
for (i = 0; i < len; i++) {
fprintf(fp, " (*F%d(%d)) %s", i, i, i == len - 1 ? ";\n" : "+");
}
fprintf(fp, "}\n");
fprintf(fp, "int __set_key(char *__p)\n{\n"
"\tchar *x = __p; \n");
for (i = 0; i < len; i++) {
key[i].rand = rand() % 5;
sprintf(key[i].buf, "\tx[I%d] = *F%d(0); \n", i, i);
}
qsort(key, len, sizeof(struct set_key), _cmp_key);
for (i = 0; i < len; i++) {
fprintf(fp, "%s", key[i].buf);
}
fprintf(fp, "\treturn fall(); \n}\n");
fclose(fp);
memset(buf, 0, len);
memset(tmp, 0, len);
return 0;
}