This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xorimage.cpp
150 lines (131 loc) · 4.16 KB
/
xorimage.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
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
/*
* This file is part of Stecrypt.
*
* Stecrypt is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Stecrypt is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Stecrypt. If not, see <http://www.gnu.org/licenses/>.
*/
#include "xorimage.h"
#include <QColor>
#include <vector>
XORImage::XORImage(unsigned int textmaxlength, QImage &i) :
text_max_length(textmaxlength), image(i)
{
error = 0;
number = 0;
}
void XORImage::writeB(bool t1, bool t2, int px) {
// calculate pixel position
int p_x = px % image.width(),
p_y = px / image.width();
QRgb colors = image.pixel(p_x, p_y);
int green = qGreen(colors), blue = qBlue(colors);
if (t1 && green % 2 != 1) green++;
else if (!t1 && green % 2 != 0) green--;
if (t2 && blue % 2 != 1) blue++;
else if (!t2 && blue % 2 != 0) blue--;
image.setPixel(p_x, p_y, qRgb(qRed(colors), green, blue));
}
void XORImage::readB(bool &t1, bool &t2, int px) {
// calculate pixel position
int p_x = px % image.width(),
p_y = px / image.width();
QRgb colors = image.pixel(p_x, p_y);
int green = qGreen(colors), blue = qBlue(colors);
t1 = green % 2 == 1;
t2 = blue % 2 == 1;
}
void XORImage::write() {
if (number == 0) {
error = 4;
return;
}
QString t = text + QChar(1);
// we must have |t| divisible by 8
while (t.length() % 8 != 0)
t += QChar(1);
// encrypting happens below
// we assume `number` is 8bytes-long `unsigned long long` type
unsigned long long n1=0, n2=0;
//QString t2(t.length(), QChar(1));
std::vector<uchar> t2(t.length(), uchar(1));
for (int i=0; i<t.length(); i+=8) {
n1 = (unsigned long long)t[i].toAscii();
for (unsigned j=1; j<8; j++) {
n1 <<= 8LL;
n1 += (unsigned long long)t[i+j].toAscii();
}
n1 = n1 ^ number; // now we have 8 bytes of text encrypted
// getting bytes from the number
for (unsigned int j=0; j<8; j++) {
n2 = n1 << j * 8;
n2 = n2 >> 7 * 8;
//t2[i+j] = QChar(int(n2));
t2[i+j] = uchar(n2);
/*if (int(n2)==224) {
t2[i+j] = QChar(224);
}*/
}
}
// now `t2` contains encrypted text
//for i in t2: this->writeB(i)
//check for image capacity
for (unsigned int i=0, px=0; i<t2.size(); i++) {
uchar c = t2[i];
std::bitset<8> bs((int)c);
for (int j=0; j<8; j+=2) {
writeB(bs.test(j), bs.test(j+1), px);
px++;
}
}
}
void XORImage::read() {
if (number == 0) {
error = 4;
return;
}
text.clear();
int px=0, size=image.width() * image.height();
bool running = true;
while (px < size && running) {
unsigned long long n1=0, n2=0;
// it takes 32 pixels to write 8 bytes of text
// because 8bytes = 64 bits and there are 2 bits per pixel
// so 32 pixels
// whereas 1 letter of text (==1B) is 4 pixels
for (int n=0; n<8; n++) {
// below is loop which reads one byte
bool t1, t2;
uchar b;
std::bitset<8> bs(0);
for (int i=0; i<4; i++, px++) {
readB(t1, t2, px);
bs.set(i*2, t1);
bs.set(i*2+1, t2);
}
b = bs.to_ulong();
n1 <<= 8;
n1 += b;
}
n1 = n1 ^ number;
// getting bytes from the number
for (unsigned int j=0; j<8; j++) {
n2 = n1 << j * 8;
n2 = n2 >> 7 * 8;
if (n2<=1) {
running = false;
break;
}
text.append(QChar(int(n2)));
}
}
}