-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpaceCrypt.php
More file actions
66 lines (58 loc) · 2.15 KB
/
Copy pathSpaceCrypt.php
File metadata and controls
66 lines (58 loc) · 2.15 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
class SpaceCrypt
{
protected $string;
public function __construct($string)
{
$this->string = $string;
}
public function encrypt($public)
{
$public = explode(' ', $public);
if (count($public) == 1) {
$public[1] = '';
} else {
$str = $this->str2bin($this->string);
$private = $this->bin2hidden($str);
$msg_start = $public[0].' '.$private;
unset($public[0]);
return $msg_start. implode(' ', $public);
}
}
public function decrypt()
{
return $this->bin2str($this->hidden2bin($this->string));
}
// Convert a string into binary data
protected function str2bin($text)
{
$bin = array();
for ($i = 0; strlen($text) > $i; $i ++)
$bin[] = decbin(ord($text[$i]));
return implode(' ', $bin);
}
// Convert binary data to a string
protected function bin2str($bin)
{
$text = array();
$bin = explode(' ', $bin);
for ($i = 0; count($bin) > $i; $i ++)
$text[] = chr(bindec($bin[$i]));
return implode($text);
}
// Convert the ones, zeros, and spaces of the hidden binary data to their respective zero-width characters
protected function bin2hidden($str)
{
$str = str_replace(' ', "\xE2\x81\xA0", $str); // Unicode Character 'WORD JOINER' (U+2060) 0xE2 0x81 0xA0
$str = str_replace('0', "\xE2\x80\x8B", $str); // Unicode Character 'ZERO WIDTH SPACE' (U+200B) 0xE2 0x80 0x8B
$str = str_replace('1', "\xE2\x80\x8C", $str); // Unicode Character 'ZERO WIDTH NON-JOINER' (U+200C) 0xE2 0x80 0x8C
return $str;
}
// Convert zero-width characters to hidden binary data
protected function hidden2bin($str)
{
$str = str_replace("\xE2\x81\xA0", ' ', $str); // Unicode Character 'WORD JOINER' (U+2060) 0xE2 0x81 0xA0
$str = str_replace("\xE2\x80\x8B", '0', $str); // Unicode Character 'ZERO WIDTH SPACE' (U+200B) 0xE2 0x80 0x8B
$str = str_replace("\xE2\x80\x8C", '1', $str); // Unicode Character 'ZERO WIDTH NON-JOINER' (U+200C) 0xE2 0x80
return $str;
}
}