-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrack.c
127 lines (118 loc) · 2.25 KB
/
crack.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
/*
* crack.c is able to encode and decode binary encoded messages using
* ascii representation.
*/
/* This routine accepts a binary string and returns it as a character */
char
binary_to_character(char *input) {
int ii;
char output = 0;
for (ii = 0; ii < 8; ii++) {
if (input[ii] == '1') {
output += (1 << (7 - ii));
}
}
return output;
}
/* Accepts a character and returns the binary encoding in the out arg. */
void
character_to_binary(char in, char *out, int len)
{
int ii;
assert(len >= 8);
for (ii = 0; ii < 8; ii++) {
if ((1 << 7) & in) {
out[ii] = '1';
} else {
out[ii] = '0';
}
in = in << 1;
}
}
FILE*
open_file(char *file) {
FILE *fptr;
if (strcmp(file, "-") == 0) {
fptr = stdin;
} else {
fptr = fopen(file, "r");
}
if (fptr == NULL) {
printf("Cannot open %s\n", file);
exit(2);
}
return fptr;
}
/*
* Here we accept a filename,open the file and call binary_to_character
* on each binary letter in the file.
*/
void
decode(char *file) {
char buf[1024];
char *token;
FILE *fptr = open_file(file);
while (fgets(buf,sizeof(buf), fptr)) {
token = (char*)strtok(buf, " ");
if (token == NULL) {
printf("No tokens\n");
break;
}
do {
printf("%c", binary_to_character(token));
token = (char *)strtok(NULL, " ");
} while(token);
}
printf("\n");
fclose(fptr);
}
/*
* Here we accept a filename, open the file and call character_to_binary
* on each ascii letter in the file.
*/
void
encode(char *file) {
char buf[1024];
int binary_count = 0;
FILE *fptr = open_file(file);
while (fgets(buf, sizeof(buf), fptr)) {
int len = strnlen(buf, 1024);
int ii;
for (ii = 0; ii < len; ii++) {
char binbuf[8];
character_to_binary(buf[ii], binbuf, 8);
printf("%s ", binbuf);
binary_count++;
/* No more than 8 on a line */
if (binary_count == 8) {
printf("\n");
binary_count = 0;
}
}
}
fclose(fptr);
printf("\n");
}
int
main(int argc, char **argv) {
int opt;
while((opt = getopt(argc, argv, "d:e:")) != -1) {
switch(opt) {
case 'd':
decode(optarg);
break;
case 'e':
encode(optarg);
break;
default:
printf("-d <file> | -e <file>\n");
break;
}
}
return 0;
}