-
Notifications
You must be signed in to change notification settings - Fork 0
/
libacm.php
313 lines (298 loc) · 9.86 KB
/
libacm.php
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
LibACM - A procedure-based library to perform Arnold's Cat Map image pixel
reposition and Logistic Map image pixel encryption.
This library provides functions to perform ACM image pixel reposition and
apply Logistic Map into image pixel.
Dependencies:
(core) PHP >= 7.2.0
(extension) EXIF >= 7.1.0 (Used to read image metadata - bundled with PHP)
(extension) GD >= 2.1.0 (Used to manipulate image - bundled with PHP)
(extension) mbstring >= 1.3.2 (Used by EXIF and GD - bundled with PHP)
(library) php-ai/php-ml >= 0.7.0 (Used to handle most of Matrix operation - Install via composer)
--- USE ONLY FOR DEVELOPMENT AND EXPERIMENT! ---
*/
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\Math\Matrix;
/*
Load image from specific file path.
Parameter:
$filepath => (string) path to image
$debug => (bool) enable debug mode (verbose output)
Return:
$img => (resource) a GD resource image
Exception:
File inaccessible or unreadable
File is not a valid image
Cannot convert image to true color
File must a square image (width = height)
*/
function load_image($filepath, $debug = false) {
if ($debug) echo "Loading File\n";
// Check if $filepath is accessible and readable
if ( ($data = file_get_contents($filepath)) !== false ) {
// Check and try read $filepath as image
if ( ($img = imagecreatefromstring($data)) !== false ) {
// Bugfix: convert $img to true color for best result processing
if ( imagepalettetotruecolor($img) != false ) {
// Check image size
$imagesize = getimagesize($filepath);
if ($imagesize[0] == $imagesize[1]) {
// Return the $img
return $img;
} else {
throw new Exception('File must a square image (width = height)');
}
} else {
throw new Exception('Cannot convert image to true color');
}
} else {
throw new Exception('File is not a valid image');
}
} else {
throw new Exception('File inaccessible or unreadable');
}
}
/*
Apply a modulo by scalar operation into matrix.
Parameter:
$mat => (Matrix) Matrix to be applied
$num => (int) divider
Return:
{} => (Matrix) Matrix of remainder
Exception:
*/
function helper_mod(Matrix $mat, $num) {
$flattened = $mat->toArray();
foreach ($flattened as &$x) {
foreach ($x as &$y) {
$y = $y % $num;
}
}
return new Matrix($flattened);
}
/*
Perform ACM reposition encryption.
Parameter:
$image => (resource, pointer) GD resource image that will be applied with ACM
$N => (int) image width or image height size
$p => (int) ACM variable: p
$q => (int) ACM variable: q
$iteration => (int) iteration count on how much ACM will be performed
$debug => (bool) enable debug mode (verbose output)
Return:
Exception:
*/
function acm_encrypt(&$image, $N, $p, $q, $iteration, $debug = false) {
if ($debug) echo "Encryption mode\n";
// Prepare environments for ACM encryption
$M = new Matrix([
[1, $p],
[$q, $p * $q + 1]
]);
if ($debug) echo "Env ready, starting iteration\n";
// Start the iteration
for ($i = 0; $i < $iteration; $i++) {
$currenttime = microtime(true);
if ($debug) echo "Iteration $i ... ";
// Generate $empty image as overlay
$empty = imagecreatetruecolor($N, $N);
for ($x = 0; $x < $N; $x++) {
for ($y = 0; $y < $N; $y++) {
// Calculate ACM
$acm = helper_mod($M->multiply(new Matrix([[$x], [$y]])), $N);
// Get ACM Values
$temp = $acm->getColumnValues(0);
// Reposition $image in $empty
imagesetpixel($empty, $x, $y, imagecolorat($image, $temp[0], $temp[1]));
}
}
// Copy $empty image into $image
imagecopy($image, $empty, 0, 0, 0, 0, $N, $N);
// Release all resource belongs to $empty
imagedestroy($empty);
if ($debug) echo "passed (" . (microtime(true) - $currenttime) . " s)\n";
}
}
/*
Perform ACM reposition decryption.
Parameter:
$image => (resource, pointer) GD resource image that will be applied with ACM
$N => (int) image width or image height size
$p => (int) ACM variable: p
$q => (int) ACM variable: q
$iteration => (int) iteration count on how much ACM will be performed
$debug => (bool) enable debug mode (verbose output)
Return:
Exception:
*/
function acm_decrypt(&$image, $N, $p, $q, $iteration, $debug = false) {
if ($debug) echo "Decryption mode\n";
// Prepare environments for ACM decryption
$M = new Matrix([
[1, $p],
[$q, $p * $q + 1]
]);
if ($debug) echo "Env ready, starting iteration\n";
// Start the iteration
for ($i = 0; $i < $iteration; $i++) {
$currenttime = microtime(true);
if ($debug) echo "Iteration $i ... ";
// Generate $empty image as overlay
$empty = imagecreatetruecolor($N, $N);
for ($x = 0; $x < $N; $x++) {
for ($y = 0; $y < $N; $y++) {
// Calculate ACM
$acm = helper_mod($M->multiply(new Matrix([[$x], [$y]])), $N);
// Get ACM Values
$temp = $acm->getColumnValues(0);
// Reposition $image in $empty
imagesetpixel($empty, $temp[0], $temp[1], imagecolorat($image, $x, $y));
}
}
// Copy $empty image into $image
imagecopy($image, $empty, 0, 0, 0, 0, $N, $N);
// Release all resource belongs to $empty
imagedestroy($empty);
if ($debug) echo "passed (" . (microtime(true) - $currenttime) . " s)\n";
}
}
/*
Apply Logistic Map into image.
Parameter:
$image => (resource, pointer) GD resource image that will be applied with ACM
$N => (int) image width or image height size
$startnumber=> (int) Logistic Map variable: x0
$r => (int) Logistic Map variable: r
$debug => (bool) enable debug mode (verbose output)
Return:
Exception:
*/
function logmap_apply(&$image, $N, $startnumber = 0.456, $r = 4, $debug = false) {
if ($debug) echo "Preparing Logistic Mapper\n";
$x = array($startnumber);
$a = 10 ** 3;
$imagesize = $N ** 2;
$i = 0;
if ($debug) echo "Logistic Mapper ready, starting map\n";
for ($shifter = 16; $shifter >= 0; $shifter -= 8) {
$channeltime = microtime(true);
if ($debug) echo "----- Mapping Channel: " . ($shifter == 16 ? 'RED' : ($shifter == 8 ? 'GREEN' : 'BLUE')) . " -----\n";
for ($row = 0; $row < $N; $row++) {
$currenttime = microtime(true);
if ($debug) echo "Row $row of $N ... ";
for ($col = 0; $col < $N; $col++) {
if ($i < $imagesize) {
// 1. Generate keystream from Logistic Map
$x[$i + 1] = $r * $x[$i] * (1 - $x[$i]);
// 2. Extract 3 digits integer starting from 2 digits after coma
$key = ($x[$i + 1] * (10 ** 5)) % (10 ** 3);
// 3. Keep $key in bytes range by apply modulo
$key = $key % 256;
// 4. Apply $key to specific color and channel using XOR
// 4.1. Create $encryptedColor
$color = imagecolorat($image, $row, $col); // Get color, result in 0xRRGGBB hex format
$shiftedKey = $key << $shifter; // Shifting $shifter bits, resulted (hex): 0xKK0000 in RED($shifter = 16), 0x00KK00 in GREEN($shifter = 8), or 0x0000KK in BLUE($shifter = 0)
$encryptedColor = $color ^ $shiftedKey; // Just XOR, other channel will remain untouched
// 4.2. Decompose $encryptedColor
$red = ($encryptedColor >> 16) & 0xFF;
$green = ($encryptedColor >> 8) & 0xFF;
$blue = ($encryptedColor) & 0xFF;
// 4.3. Assign $encryptedColor
$encryptedColorId = imagecolorexact($image, $red, $green, $blue); // Check if the color already allocated
if ($encryptedColorId == -1) {
$encryptedColorId = imagecolorallocate($image, $red, $green, $blue); // Allocate if color not exist
}
imagesetpixel($image, $row, $col, $encryptedColorId);
$i += 1;
}
}
if ($debug) echo "Completed (" . (microtime(true) - $currenttime) . " s)\n";
}
if ($debug) echo "----- Channel Mapping Completed (" . (microtime(true) - $channeltime) . " s) -----\n";
}
}
/*
Write image.
Parameter:
$image => (resource, pointer) GD resource image that will be applied with ACM
$imagetype => (int) image type defined in EXIF (Currently support: IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_WBMP, IMAGETYPE_WEBP, IMAGETYPE_XBM, IMAGETYPE_BMP)
$destination=> (string) Pah to file where image will be written
$destroy => (bool) indicates whether destroy the image resource on completion of image writing or not
$debug => (bool) enable debug mode (verbose output)
Return:
Exception:
*/
function write_image(&$image, $imagetype, $destination = null, $destroy = true, $debug = false) {
if ($debug) echo "Writing ...\n";
switch ($imagetype) {
case IMAGETYPE_GIF: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_GIF));
imagegif($image);
} else {
imagegif($image, $destination);
}
break;
}
case IMAGETYPE_JPEG: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_JPEG));
imagejpeg($image);
} else {
imagejpeg($image, $destination);
}
break;
}
case IMAGETYPE_PNG: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_PNG));
imagepng($image);
} else {
imagepng($image, $destination);
}
break;
}
case IMAGETYPE_WBMP: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_WBMP));
imagewbmp($image);
} else {
imagewbmp($image, $destination);
}
break;
}
case IMAGETYPE_WEBP: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_WEBP));
imagewebp($image);
} else {
imagewebp($image, $destination);
}
break;
}
case IMAGETYPE_XBM: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_XBM));
imagexbm($image);
} else {
imagexbm($image, $destination);
}
break;
}
default: {
if (is_null($destination)) {
//header('Content-Type: ' . image_type_to_mime_type(IMAGETYPE_BMP));
imagebmp($image);
} else {
imagebmp($image, $destination);
}
break;
}
}
if ($destroy) {
if ($debug) echo "Cleaning ...";
imagedestroy($image);
if ($debug) echo " Done\n";
}
}