forked from AgustinNaso/TP-Cripto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.c
242 lines (227 loc) · 7.46 KB
/
embed.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "include/fileHandler.h"
#include "include/types.h"
#include "include/encrypt.h"
#include "include/utils.h"
// #include <openssl/aes.h>
void lsb4(unsigned char msgByte, FILE *input, FILE *output, int groups[4][2])
{
unsigned char inputFileByte;
int i = 8;
fread(&inputFileByte, 1, 1, input);
for (; i > 4; i--)
{
char currBit = GET_NTH_LSB(msgByte, i);
inputFileByte = modifyBit(inputFileByte, i -1 - 4 , currBit);
}
fwrite(&inputFileByte, 1, 1, output);
fread(&inputFileByte, 1, 1, input);
for (; i > 0; i--)
{
char currBit = GET_NTH_LSB(msgByte, i);
inputFileByte = modifyBit(inputFileByte, i - 1, currBit);
}
fwrite(&inputFileByte, 1, 1, output);
}
void lsb1(unsigned char msgByte, FILE *input, FILE *output, int groups[4][2])
{
unsigned char inputFileByte;
for (int i = 8; i > 0; i--)
{
fread(&inputFileByte, 1, 1, input);
char currBit = GET_NTH_LSB(msgByte, i);
// Insert currBit in least significant bit of inputFileByte
inputFileByte = modifyBit(inputFileByte, 0, currBit);
fwrite(&inputFileByte, 1, 1, output);
}
}
void lsbiScan(unsigned char msgByte, FILE *input, FILE *output, int groups[4][2])
{
unsigned char inputFileByte;
for (int i = 7; i >= 0; i--)
{
fread(&inputFileByte, 1, 1, input);
char currBit = GET_NTH_LSB(msgByte, i);
if (groups[0][0] != -1)
{
char LSB = GET_NTH_LSB(inputFileByte, 1);
char secondLSB = GET_NTH_LSB(inputFileByte, 2);
char thirdLSB = GET_NTH_LSB(inputFileByte, 3);
groups[GET_INT_FROM_2_BITS(thirdLSB, secondLSB)][LSB == currBit]++;
}
}
}
void lsbiInsert(unsigned char msgByte, FILE *carrier, FILE *output, int groups[4][2])
{
unsigned char inputFileByte;
for (int i = 8; i > 0; i--)
{
fread(&inputFileByte, 1, 1, carrier);
char currBit = GET_NTH_LSB(msgByte, i);
if (groups[0][0] != -1)
{
// char LSB = GET_NTH_LSB(inputFileByte, 1);
char secondLSB = GET_NTH_LSB(inputFileByte, 2);
char thirdLSB = GET_NTH_LSB(inputFileByte, 3);
if (groups[GET_INT_FROM_2_BITS(thirdLSB, secondLSB)][MATCHING] < groups[GET_INT_FROM_2_BITS(thirdLSB, secondLSB)][NON_MATCHING])
currBit = ~currBit & 1u;
}
inputFileByte = modifyBit(inputFileByte, 0, currBit);
fwrite(&inputFileByte, 1, 1, output);
}
}
void embed(const char *bmpPath, const char *filePath, const char *outBmpName, int lsbType, int hasEncryption)
{
FILE *carrier = fopen(bmpPath, "r");
FILE *fileToEmbed = fopen(filePath, "r");
void (*chosenStegAlgorithm)(unsigned char, FILE *, FILE *, int(*)[2]);
int groups[4][2] = {{-1}};
switch (lsbType)
{
case LSBI:
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 2; j++)
groups[i][j] = 0;
chosenStegAlgorithm = lsbiScan;
break;
}
case LSB1:
chosenStegAlgorithm = lsb1;
break;
case LSB4:
chosenStegAlgorithm = lsb4;
break;
default:
break;
}
const char *fileExtension = getFileExtension(filePath);
fseek(fileToEmbed, 0L, SEEK_END);
uint32_t sz = ftell(fileToEmbed);
rewind(fileToEmbed);
FILE *output = fopen(outBmpName, "wb");
bmpFileHeader bfh;
bmpImageHeader bih;
fread(&bfh, 1, 14, carrier);
fread(&bih, 1, sizeof(bih), carrier);
fwrite(&bfh, 1, 14, output);
fwrite(&bih, 1, sizeof(bih), output);
uint32_t headers_position = ftell(output);
// Embed file size
unsigned char byte;
if (lsbType == LSBI)
{
fseek(carrier, headers_position + 4, SEEK_SET);
}
for (char i = 0; i < 4; i++)
{
byte = (sz >> (8 * (3 - i))) & 0xff;
chosenStegAlgorithm(byte, carrier, output, groups);
}
// Embed fileToEmbed
while (fread(&byte, 1, sizeof(byte), fileToEmbed) == 1)
chosenStegAlgorithm(byte, carrier, output, groups);
// Embed extension
if (hasEncryption == 0)
{
for (int i = 0; i < strlen(fileExtension); i++)
chosenStegAlgorithm(fileExtension[i], carrier, output, groups);
}
chosenStegAlgorithm('\0', carrier, output, groups);
if (lsbType == LSBI)
{
fseek(carrier, headers_position, SEEK_SET);
rewind(fileToEmbed);
// Inserting which patterns suffered modifications
for (int i = 0; i < 4; i++)
{
fread(&byte, 1, 1, carrier);
char groupBit = groups[i][MATCHING] < groups[i][NON_MATCHING];
byte = modifyBit(byte, 0, groupBit);
fwrite(&byte, 1, 1, output);
}
for (char i = 0; i < 4; i++)
{
byte = (sz >> (8 * (3 - i))) & 0xff;
lsbiInsert(byte, carrier, output, groups);
}
// Embed fileToEmbed
while (fread(&byte, 1, sizeof(byte), fileToEmbed) == 1)
lsbiInsert(byte, carrier, output, groups);
// Embed extension
if (hasEncryption == 0)
{
for (int i = 0; i < strlen(fileExtension); i++)
lsbiInsert(fileExtension[i], carrier, output, groups);
}
lsbiInsert('\0', carrier, output, groups);
}
// Copy the remaining data
while (fread(&byte, 1, sizeof(byte), carrier))
fwrite(&byte, 1, 1, output);
fclose(fileToEmbed);
fclose(carrier);
fclose(output);
if (hasEncryption)
remove(filePath);
}
int checkCarrierSize(char * fileToEmbed, char * carrierPath, int stegMode){
FILE * fp = fopen(fileToEmbed, "r+");
fseek(fp, 0L, SEEK_END);
uint32_t fileToEmbedSize = ftell(fp);
fileToEmbedSize+= strlen(getFileExtension(fileToEmbed));
fclose(fp)
fp = fopen(carrierPath, "r+");
fseek(fp, 0L, SEEK_END);
uint32_t carrierSize = ftell(fp);
fclose(fp)
int sizeDifference =-1;
switch (stegMode)
{
case LSB1:
sizeDifference = carrierSize - fileToEmbedSize * 8;
break;
case LSB4:
sizeDifference = carrierSize - fileToEmbedSize * 4;
break;
case LSBI:
sizeDifference = carrierSize - (fileToEmbedSize * 8 + 4)
break;
default:
break;
}
return sizeDifference;
}
int handleEmbedding(char * fileToEmbed, char * carrierPath, char * embeddedFileName, int embedMode, int encryptAlgo, int encryptMode, unsigned char * password)
{
if(checkCarrierSize(fileToEmbed, carrierPath, embed) < 0){
printf("File to embed doesn't fit in carrier\n");
return -1;
}
FILE * fp = fopen(fileToEmbed, "r+");
fseek(fp, 0L, SEEK_END);
uint32_t sz = ftell(fp);
rewind(fp);
const char * extension = getFileExtension(fileToEmbed);
int dataLen = sz + 4 + strlen(extension) + 1;
unsigned char * data = malloc(sizeof(char) * (dataLen));
sizeTo4ByteArray(sz, data);
fread(data + 4, sz, 1, fp);
for(int i = 0 ; i < strlen(extension); i++)
data[sz + 4 + i] = extension[i];
char * filePath = fileToEmbed;
int encrypted = 1;
if(encryptMode == 0) encrypted = 0;
printf("\n");
for(int i = 4 ; i< sz + 4 + strlen(extension); i++)
printf("%c", data[i]);
data[sz + 4 + strlen(extension)] = 0;
if(encrypted != 0)
filePath = encrypt(password, data, encryptAlgo, encryptMode, dataLen);
embed(carrierPath, filePath, embeddedFileName, embedMode, encrypted);
fclose(fp);
free(data);
return 0;
}