-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.php
47 lines (34 loc) · 1.04 KB
/
utils.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
<?php
function tp_decrypt ( $cypher_text , $first_key = 0xAB ) {
$header = substr($cypher_text, 0, 4);
$header_length = unpack('N*', $header)[1];
$cypher_text = substr($cypher_text, 4);
$buf = unpack('c*', $cypher_text );
$key = $first_key;
$nextKey;
for ($i = 1; $i < count($buf)+1; $i++) {
$nextKey = $buf[$i];
$buf[$i] = $buf[$i] ^ $key;
$key = $nextKey;
}
$array_map = array_map('chr', $buf);
$clear_text = implode('', $array_map);
$cypher_length = strlen($clear_text);
if ($header_length !== $cypher_length) {
trigger_error("Length in header ({$header_length}) doesn't match actual message length ({$cypher_length}).");
}
return $clear_text;
}
function tp_encrypt ( $clear_text , $first_key = 0xAB ) {
$buf = unpack('c*', $clear_text );
$key = $first_key;
for ($i = 1; $i < count($buf)+1; $i++) {
$buf[$i] = $buf[$i] ^ $key;
$key = $buf[$i];
}
$array_map = array_map('chr', $buf);
$clear_text = implode('', $array_map);
$length = strlen($clear_text);
$header = pack('N*', $length);
return $header . $clear_text;
}