-
Notifications
You must be signed in to change notification settings - Fork 1
/
chal2.c
104 lines (85 loc) · 1.42 KB
/
chal2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char secret1[8] = {4,13,15,6,21,25,82,19};
char secret2[8] = {45,8,0,47,20,20,29,13};
void fail(char *);
int check1(char *string) {
char *a = string;
int i;
for (i=0;i<=7;i++) {
switch(i%6) {
case 0:
if ((a[i]^'b')!=secret1[i]) {
fail(a);
}
break;
case 1:
if ((a[i]^'a')!=secret1[i]) {
fail(a);
}
break;
case 2:
if ((a[i]^'n')!=secret1[i]) {
fail(a);
}
break;
case 3:
if ((a[i]^'a')!=secret1[i]) {
fail(a);
}
break;
case 4:
if ((a[i]^'n')!=secret1[i]) {
fail(a);
}
break;
case 5:
if ((a[i]^'a')!=secret1[i]) {
fail(a);
}
break;
}
printf("Char %c\n",a[i]);
}
return(1);
}
int check2(char *string) {
char *a = string;
char b[] = "rasp";
int i;
for (i=8;i<=15;i++) {
if ((a[i]^b[i%4]) != secret2[i-8]) {
fail(a);
}
printf("Char %c\n",a[i]);
}
return 1;
}
void fail(char *string) {
printf("Sorry %s is not correct\n",string);
exit(-1);
}
void hurray(char *string) {
printf("Hurray! %s is correct!\n",string);
exit(0);
}
void usage(char **argv) {
printf("Usage: %s <key>\n",argv[0]);
exit(-1);
}
int main (int argc, char **argv) {
if (argc<=1) {
usage(argv);
}
if (strnlen(argv[1],1000)!=16) {
fail(argv[1]);
}
if (!check1(argv[1])) {
fail(argv[1]);
}
if (!check2(argv[1])) {
fail(argv[1]);
}
hurray(argv[1]);
}