forked from JoppeDC/Stega-in-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.php
93 lines (75 loc) · 2.06 KB
/
decrypt.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
<?php
//Convert string to binary
function toBin($str)
{
$str = (string)$str;
$l = strlen($str);
$result = '';
while ($l--) {
$result = str_pad(decbin(ord($str[$l])), 8, "0", STR_PAD_LEFT) . $result;
}
return $result;
}
//Convert binary to string
function toString($str)
{
$text_array = explode("\r\n", chunk_split($str, 8));
$newstring = '';
for ($n = 0; $n < count($text_array) - 1; $n++) {
$newstring .= chr(base_convert($text_array[$n], 2, 10));
}
return $newstring;
}
function decryptSteno($src) {
$img = imagecreatefrompng($src);
$real_message = '';
$count = 0;
$pixelX = 0;
$pixelY = 0;
list($width, $height, $type, $attr) = getimagesize($src);
while ($pixelY < $height) {
$rgb = imagecolorat($img, $pixelX, $pixelY);
$b = $rgb & 0xFF;
$blue = toBin($b);
$real_message .= $blue[strlen($blue) - 1];
$count++;
if ($count === 8) {
if (toString(substr($real_message, -8)) === '|') {
$real_message = toString(substr($real_message, 0, -8));
return [
"type" => "success",
"message" => "Success decrypting.",
"content" => json_decode($real_message)
];
}
$count = 0;
}
$pixelX++;
if ($pixelX === $width) {
$pixelY++;
$pixelX = 0;
}
if ($pixelY === $height && $pixelX === $width) {
return [
"type" => "error",
"message" => "Max Reached"
];
}
}
return [
"type" => "error",
"message" => "Decryption failed"
];
}
$return_data = [
"type" => null,
"message" => null
];
if (isset($_FILES['image_with_data'])) {
$return_data = decryptSteno($_FILES['image_with_data']['tmp_name']);
} else {
$return_data["type"] = "error";
$return_data["message"] = "Enter a file please.";
}
header('Content-Type: application/json');
echo json_encode($return_data);