-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleibniz.class.php
203 lines (178 loc) · 4.68 KB
/
leibniz.class.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
<?php
class Leibniz
{
public $alphabets = NULL;
public $gear = NULL;
public $debug = false;
const default_alphabet = 'ABCDEFGHIKLMNOPQRSTUWXYZ';
function __construct()
{
$this->gear = array();
$this->alphabets = array();
}
public function alph_string_to_array($line)
{
if(strlen($line) == strlen(self::default_alphabet))
{
$new_alph = array();
for($i = 0; $i < strlen($line); $i++)
{
// 'A' == 65
$new_alph[substr(self::default_alphabet,$i,1)] = $line[$i];
}
return $new_alph;
}
else
return false;
}
public function set_gear($line)
{
for($i = 0; $i < strlen($line); $i++)
$this->gear[] = $line[$i];
if($this->debug)
$this->pre_array($this->gear);
}
public function read_gear_file($filename = "gears.txt")
{
$lines = explode("\n",file_get_contents($filename));
$line = preg_replace('/[[:space:]]/','',$lines[0]);
$this->set_gear($line);
}
public function alphabets_string_to_array($string, $delimiter = "\n")
{
$lines = explode($delimiter, $string);
$alphabets = array();
foreach($lines as $line)
{
$line = preg_replace('/[[:space:]]/','', $line);
if(($new_alph = $this->alph_string_to_array($line)) !== false)
{
$alphabets[count($alphabets)] = $new_alph;
}
}
return $alphabets;
}
public function set_alphabets($alphabets, $delimiter = "\n")
{
$this->alphabets = $this->alphabets_string_to_array($alphabets, $delimiter);
if($this->debug)
$this->pre_array($this->alphabets);
}
public function read_cyphers_file($filename = "cyphers.txt")
{
$this->set_alphabets(file_get_contents($filename));
}
public function encrypt($msg)
{
$msg = $this->pad($msg);
$enc_msg = '';
$cur_alph = 0;
for($i = 0; $i < strlen($msg); $i++)
{
$char = $msg[$i];
if(!isset($this->alphabets[$cur_alph][$char]))
$this->error("Unexpected input: $char is not in the character set");
$enc_char = $this->alphabets[$cur_alph][$char];
if($this->gear[$i % count($this->gear)] == 1)
$cur_alph = ($cur_alph + 1) % count($this->alphabets);
$enc_msg .= $enc_char;
if($this->debug)
echo $char . " to " . $enc_char . "<br>";
}
return $enc_msg;
}
public function decrypt($msg, $alphabets = NULL, $gear = NULL)
{
$msg = $this->pad($msg);
$dec_msg = '';
$cur_alph = 0;
if($alphabets === NULL)
$alphabets = $this->alphabets;
if($gear === NULL)
$gear = $this->gear;
for($i = 0; $i < strlen($msg); $i++)
{
$enc_char = $msg[$i];
$dec_char = NULL;
foreach($alphabets[$cur_alph] as $dec => $enc)
{
if($enc_char == $enc)
{
$dec_char = $dec;
break;
}
}
if($dec_char === NULL)
$this->error("Unexpected input: $enc_char is not in the character set");
if($gear[$i % count($gear)] == 1)
$cur_alph = ($cur_alph + 1) % count($alphabets);
$dec_msg .= $dec_char;
if($this->debug)
echo $enc_char . " to " . $dec_char . "<br>";
}
return $dec_msg;
}
public function crack($encrypted_message, $original_message, $know_alphabets = true, $know_gear = false)
{
if($know_gear)
$possible_gears = $this->gear;
else
$possible_gears = $this->enumerate_gears();
$attempts = 0;
if($know_alphabets)
foreach($possible_gears as $gear)
{
$attempts++;
if($this->decrypt($encrypted_message, $this->alphabets, $gear) == $decrypted_message)
break;
}
else
{
// realized that there are (24!)^6 permutations, way way too many
// so we'll just apologize and refuse to try.
$this->error('you ask the impossible');
/*
foreach($possible_gears as $gear)
$attempts += $this->crack_permuting_alphabets($encrypted_message, $decrypted_message, $gear, $attempts);
*/
}
return $attempts;
}
public function crack_permuting_alphabets($encrypted_message, $decrypted_message, $gear, $attempts)
{
$alphabets = $this->generate_starting_alphabets_string();
}
public function generate_starting_alphabets_string($num = 6, $delimiter = "\n")
{
$alphabets = array();
for($i = 0; $i < $num; $i++)
$alphabets[] = self::default_alphabet;
return implode($delimiter, $alphabets);
}
public function enumerate_gears($length = 6)
{
$gears = array();
for($i = 0; $i < pow(2,6); $i++)
$gears[] = decbin($i);
return $gears;
}
public function error($msg, $die = true)
{
echo $msg;
if($die)
die();
}
public function pre_array($arr)
{
echo "<pre>";
print_r($arr);
echo "</pre>";
}
public function pad($msg)
{
$msg = preg_replace('/[[:punct:]]/','', $msg);
$msg = preg_replace('/[[:space:]]/','', $msg);
return strtoupper($msg);
}
}
?>