-
Notifications
You must be signed in to change notification settings - Fork 0
/
duck.c
198 lines (145 loc) · 4.61 KB
/
duck.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include "bitwise_op.c"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SIGNATURE_1 0x0A
#define SIGNATURE_2 0xA0
#define SIGNATURE_3 0x5A
#define SIGNATURE_4 0x95
#define SIGNATURE_LENGTH 4
FILE * OpenFile(char FileName[], char * mode);
int Duckfile(FILE * fptr_in, FILE * fptr_out);
int FileIsDucked(FILE * fptr_in);
void MarkFileAsDucked(FILE * target);
FILE * OpenFile(char FileName[], char *mode){
FILE *myfile = NULL;
myfile = fopen(FileName,mode);
return myfile;
}
int FileIsDucked(FILE * fptr_in){
unsigned int signatures[SIGNATURE_LENGTH] = {SIGNATURE_1,SIGNATURE_2,SIGNATURE_3,SIGNATURE_4};
int i;
for(i=0;i<SIGNATURE_LENGTH;i++){
if(fgetc(fptr_in) != signatures[i]){
//sequence has mismatch - file has not been ducked
fseek(fptr_in,0,SEEK_SET);
return 0;
}
}
//file has been ducked
return 1;
}
void MarkFileAsDucked(FILE * target){
unsigned int signatures[SIGNATURE_LENGTH] = {SIGNATURE_1,SIGNATURE_2,SIGNATURE_3,SIGNATURE_4};
fseek(target,0,SEEK_SET);
int i;
for(i=0;i<SIGNATURE_LENGTH;i++){
fputc(signatures[i],target);
}
}
int main(int argc, const char * argv[])
{
if(argc < 2){
printf("Bad argument\n");
return 0;
}
FILE * fptr_in = OpenFile(argv[1], "rb");
char * outputname = malloc(sizeof(char) * 5 + sizeof(char)*10);
strcpy(outputname, argv[1]);
strcat(outputname, ".d"); //for debugging purpose
if(fptr_in == NULL ){
printf("Unable to read file. File may not exist.\n");
return 0;
}
FILE * fptr_out = OpenFile(outputname, "wb");
if(fptr_out == NULL){
printf("Unable to write file. Check folder permission.\n");
return 0;
}
int ducking_mode = Duckfile(fptr_in,fptr_out);
printf("Saved encrypted file as %s\n",outputname);
fclose(fptr_in);
fclose(fptr_out);
if(ducking_mode == 0){
char prev;
FILE * preview_ptr = OpenFile(outputname, "rb");
printf("\n\n ======== Unducked Contents ========\n\n");
do{
prev = fgetc(preview_ptr);
printf("%c",prev);
}while(prev != EOF);
fclose(preview_ptr);
}
printf("\n");
return 0;
}
void PopulateKeyholeArray(int * keyholes, char * duckingkey){
//Create boolean keyhole array used to determine whether current bit should be flipped
int i,j=0;
for(i = 0; i < strlen(duckingkey); i++){
keyholes[i] = (int)(duckingkey[j] & 0x01);
if(j++ > 8){
j=0;
}
}
}
/*
How is the Ducking-Key used. The ducking key is converted into binary, for example "foobar" will become "011001100110111101101111011000100110000101110010"
This combination will be used to feed the flipping mechanism.
Example.
[011001100110111101101111011] Ducking-Key is masked with the data continuously
Data: 0110100001100101011011000110|1100 at any 1s, the data bit is flipped
011011110010000..
[011001100110111101..] Until the data file is exhausted
1100011011110010000
*/
int Duckfile(FILE * fptr_in, FILE * fptr_out){
char duck_key[64];
srand(time(NULL));
int ducking_mode = 0;
if(!FileIsDucked(fptr_in)){
printf("\n[ENCRYPTING] Please specify the Ducking Key (do not lose this key): ");
ducking_mode = 1;
}else{
printf("\n[REVERSING] Please specify the Ducking Key to unduck this file: ");
}
scanf("%s",duck_key);
if(ducking_mode){
MarkFileAsDucked(fptr_out);
}
printf("\n[ PROCESSING ] Ducking file right here right now..\n");
int * keyholes = malloc(sizeof(int) * strlen(duck_key));
PopulateKeyholeArray(keyholes, duck_key);
int bytecount = 0;
unsigned int block;
unsigned char byte_ducked;
while(block != EOF){
byte_ducked = 0x00; //container for the byte we're going to fill up
block = fgetc(fptr_in);
if(block == EOF){
break;
}
int i = 0, j =0;
for(i = 1; i <= 8; i++){
bytecount += 1;
int writebit = getBitFromByte(i, block);
int FLIP_THIS_BIT = keyholes[j];
if(FLIP_THIS_BIT){
//If the Ducking-Key tells you to duck this bit, do it
byte_ducked = FillBit(byte_ducked, (writebit == 0 ? 1 : 0), i);
}else{
//Leave intact otherwise
byte_ducked = FillBit(byte_ducked, writebit, i);
}
j = i;
if(j > strlen(duck_key)){
j = 0;
}
}
fputc(byte_ducked,fptr_out);
}
printf("\n");
printf("%d Bytes Can Fly!\n", bytecount);
return ducking_mode;
}