-
Notifications
You must be signed in to change notification settings - Fork 3
/
XORCipher.php
59 lines (43 loc) · 1.25 KB
/
XORCipher.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
<?php
class XORCipher {
public function cipher($plaintext, $key) {
$key = $this->text2ascii($key);
$plaintext = $this->text2ascii($plaintext);
$keysize = count($key);
$input_size = count($plaintext);
$cipher = "";
for ($i = 0; $i < $input_size; $i++)
$cipher .= chr($plaintext[$i] ^ $key[$i % $keysize]);
return $cipher;
}
public function crack($cipher, $keysize) {
$cipher = $this->text2ascii($cipher);
$occurences = $key = array();
$input_size = count($cipher);
for ($i = 0; $i < $input_size; $i++) {
$j = $i % $keysize;
if (++$occurences[$j][$cipher[$i]] > $occurences[$j][$key[$j]])
$key[$j] = $cipher[$i];
}
return $this->ascii2text(array_map(function($v) { return $v ^ 32; }, $key));
}
public function plaintext($cipher, $key) {
$key = $this->text2ascii($key);
$cipher = $this->text2ascii($cipher);
$keysize = count($key);
$input_size = count($cipher);
$plaintext = "";
for ($i = 0; $i < $input_size; $i++)
$plaintext .= chr($cipher[$i] ^ $key[$i % $keysize]);
return $plaintext;
}
private function text2ascii($text) {
return array_map('ord', str_split($text));
}
private function ascii2text($ascii) {
$text = "";
foreach($ascii as $char)
$text .= chr($char);
return $text;
}
}