-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptImage.cpp
More file actions
144 lines (102 loc) · 4.43 KB
/
EncryptImage.cpp
File metadata and controls
144 lines (102 loc) · 4.43 KB
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
/*
Compile with 'g++ -o Name Name.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11'
The program encrypts a message inside a given image at a given channel (r, g, b).
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "CImg.h"
using namespace cimg_library;
#define MASK 254 //Mask to take last bit of a character.
FILE *FIn;
//This function helps to print every single bit of a variable given the size in bytes.
void printBits(size_t const size, void const * const ptr){
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i=size-1;i>=0;i--)
{
for (j=7;j>=0;j--)
{
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}
int main(int argc, char *argv[]) {
if(argc < 5){
printf("Usage: %s <image> <file with the message> <channel to corrupt> <name of the new image>\n", argv[0]);
exit(0);
}
short channel = atoi(argv[3]); //Which starting channel to encrypt (0 for red, 1 for green, 2 for blue).
char *message; //The message to encrypt.
FIn = fopen(argv[2], "r");
if(FIn != NULL){ //Read the message in the file.
if(fseek(FIn, 0L, SEEK_END) == 0){
long bufsize = ftell(FIn);
message = (char*)malloc(sizeof(char) * (bufsize)); //Allocating memory for the message.
if( fseek(FIn, 0L, SEEK_SET) != 0 ){
fputs("Error in fseek(FIn, 0L, SEEK_SET)", stderr);
}
size_t newLen = fread(message, sizeof(char), bufsize, FIn); //Copy the text in the file in the message.
if( ferror(FIn) != 0){
fputs("Error while reading file", stderr);
}
}
fclose(FIn);
}else{
fputs("Error while opening the file", stderr);
}
CImg<unsigned char> image(argv[1]), secret(argv[1]); //Read the image
short shift = 0; //Which bit to take in consideration.
int index = 0; //Which character to take in consideration.
char character; //The character to encrypt.
for( int w = 0; w < image.width(); ++w ){
for( int h = 0; h < image.height(); ++h ){
if(index < strlen(message)+1 || shift != 8){ //If we can encrypt another character of the message
//or we have to encrypt the null character we can go.
if(index == strlen(message) && shift == 8){ //IF we are at the end of the message we put the null character.
character = '\0';
++index;
}
shift = shift % 8;
channel = channel % 3;
if(shift == 0 && index < strlen(message)){ //Load next character.
character = message[index++];
}
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
printf("[DEBUG] character = %c shift = %d\n", character, shift);
unsigned char *c = image.data(w, h, 0, channel); //Original value of the pixel.
printf("[DEBUG] image(%d, %d, 0, %d) = ", w, h, channel);
printBits(sizeof(unsigned char), c);
unsigned char flag = (character & (1 << shift)); //Obtain the bit of the character we want.
flag >>= shift; //Putting at the first position so we can have 1 or 0
flag = flag | MASK; //Creating the mask (flag can be 254 if last bit was 0 or 255 if the last bit was 1).
printf("\tflag = %d\n", flag);
if(*secret.data(w, h, 0, channel) % 2 == 0){ //If the pixel has 0 as first bit
if(flag % 2 != 0){ //Change the value if flag is 255.
secret(w, h, 0, channel) = *secret.data(w, h, 0, channel) + 1;
}
}
else{ //Otherwise if the pixel has 1 as first bit
if(flag % 2 == 0){ //Change if flag is 254.
secret(w, h, 0, channel) = *secret.data(w, h, 0, channel) - 1;
}
}
printf("[DEBUG] secret(%d, %d, 0, %d) = ", w, h, channel);
printBits(1, &secret(w, h, 0, channel));
++shift;
++channel;
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
}
}
}
//secret.display(secret_disp);
//image.display(original_disp);
secret.save_png(argv[4]); //Save the encrypted image.
printf("\n[UPDATE] Secret message succesfully wrote in the image\n");
free(message);
//while(!original_disp.is_closed() && !secret_disp.is_closed()){}
return 0;
}