forked from marcinmajkowski/steganography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b_decoder.cpp
115 lines (97 loc) · 3.34 KB
/
b_decoder.cpp
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
// Scrambling the Signal - decoder
// Usage: program_name carrier encoded decoded
// Description
// This program uses user password seeded random number generator to decode
// message hidden in encoded image produced with corresponding encoder.
// Author: Marcin Majkowski, [email protected]
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cv.h>
#include <highgui.h>
using namespace cv;
using namespace std;
unsigned long hash_djb2(const char* str);
int main(int argc, char* argv[])
{
if (argc != 4) { // incorrect number of arguments
cout << "Usage: program_name carrier message encoded" << endl;
return -1;
}
// loading carrier image
cout << "Loading carrier image (" << argv[1] << ")... ";
auto carrier = Mat_<uchar>(imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE));
if (!carrier.data) {
cout << "Could not open or find " << argv[1] << endl;
return -1;
}
cout << "done" << endl;
// loading message image
cout << "Loading encoded image (" << argv[2] << ")... ";
auto encoded = Mat_<uchar>(imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE));
if (!encoded.data) {
cout << "Could not open or find " << argv[2] << endl;
return -1;
}
cout << "done" << endl;
// checking dimension agreement
cout << "Checking dimension agreement... ";
if (carrier.size == encoded.size)
cout << "done (agreement)" << endl;
else {
cout << "done (disagreement)" << endl;
cout << "Images have different dimension" << endl;
return -1;
}
// prompting user for a character string password
std::cout << "Input password: ";
std::string password;
getline(std::cin, password);
// transforming password string to a 64-bit integer seed (with hash
// function)
auto seed = hash_djb2(password.c_str());
// generating a random shuffled vector of increasing indexes for all of the
// points in an image
cout << "Generating shuffled vector of point indexes... ";
std::vector<int> indexes(encoded.cols * encoded.rows);
{ // I use block to limit scope of i
int i = 0;
for (auto& index : indexes)
index = i++;
}
RNG rng(seed);
random_shuffle(indexes.begin(), indexes.end(), rng);
cout << "done" << endl;
// generating encoded image
cout << "Generating encoded image... ";
auto decoded = carrier.clone();
auto carrier_pixel = *(carrier.begin());
auto encoded_pixel = *(encoded.begin());
{ // I use block to limit scope of i
int i = 0;
for (auto& pixel : decoded) {
carrier_pixel = *(carrier.begin() + indexes[i]);
encoded_pixel = *(encoded.begin() + indexes[i]);
pixel = (encoded_pixel - carrier_pixel) ? 0 : 255;
++i;
}
}
cout << "done" << endl;
// saving generated image
cout << "Saving decoded image (" << argv[3] << ")... ";
vector<int> compression_params = {CV_IMWRITE_PNG_COMPRESSION, 9};
imwrite(argv[3], decoded, compression_params);
cout << "done" << endl;
// success
return 0;
}
// from http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash_djb2(const char* str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}