-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrsa.php
338 lines (322 loc) · 8.08 KB
/
rsa.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* RSA Library
*
* A PHP implementation of the RSA public-private key encryption and digital signing, including generation of new
* keypairs.
* This class uses the Binary Calculator functions in PHP due to the possibility of the primes being so large that PHP
* internals not being able to cope.
*
* @category Libraries
* @package Cryptography
* @subpackage RSA
*/
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'library.php';
class RSA extends library {
protected $theirs = false,
$yours = false;
/**
* Constructor function
* Doesn't do anything, we don't have any of the required keys yet.
*/
protected function __construct() {
// Setup.
}
/**
* Generate Keypair
*
* Generate an RSA keypair from two prime numbers that are provided. The numbers given are not checked other
* than that they are integers. It is up to the user to make sure the integers passes are prime, are not the
* same, and are of the same bit-length.
*
* @access public
* @param string $prime_one
* @param string $prime_two
* @return object
*/
public function generate($prime_one, $prime_two) {
if(
!is_string($prime_one)
|| !is_string($prime_two)
|| !preg_match('/^[0-9]+$/', $prime_one)
|| !preg_match('/^[0-9]+$/', $prime_two)
) {
return false;
}
// Start generating key pair.
// Find the modulus of the two primes.
$modulus = bcmul($prime_one, $prime_two);
// Find the modulus of one minus each prime.
$calcbit = bcmul(
bcsub($prime_one, 1),
bcsub($prime_two, 1)
);
// Find the coprime. This is the public key.
$public = $this->coprime($calcbit);
// Find the private key by using the extended Euclid algorithm.
$private = $this->extend($public, $calcbit);
// Return the keypair, and its modulus in as a key object.
return $this->key($public, $private, $modulus);
}
/**
* Greatest Common Divisor
*
* @access protected
* @param string $e
* @param string $modulus
* @return string
*/
protected function gcd($e, $modulus) {
$a = $e;
$b = $modulus;
while(bccomp($a, 0) != 0) {
// Modulus fraction.
$c = bcsub(
$b,
bcmul(
$a,
bcdiv($b, $a, 0)
)
);
$b = $a;
$a = $c;
}
return $b;
}
/**
* Calculate Co-prime
*
* The following conditions are assumed:
* "GCD($modulus,$public)=1" AND "1<$public<$modulus".
*
* @access protected
* @param string $calcbit
* @return string(integer)
*/
protected function coprime($calcbit) {
$e = '3';
if(bccomp($this->gcd($e, $calcbit), '1') != 0) {
$e = '5';
$step = '2';
while(bccomp($this->gcd($e, $calcbit), '1') != 0) {
$e = bcadd($e, $step);
if($step === '2') {
$step = '4';
}
else {
$step = '2';
}
}
}
return $e;
}
/**
* Calculate private key.
*
* @access protected
* @param string $public
* @param string $calcbit
* @return string(integer)
*/
protected function extend($public, $calcbit) {
$u = array('1', '0', $calcbit);
$v = array('0', '1', $public);
while(bccomp($v[2], 0) != 0) {
$q = bcdiv($u[2], $v[2], 0);
$t = array(
bcsub($u[0], bcmul($q, $v[0])),
bcsub($u[1], bcmul($q, $v[1])),
bcsub($u[2], bcmul($q, $v[2])),
);
$u[0] = $v[0];
$u[1] = $v[1];
$u[2] = $v[2];
$v[0] = $t[0];
$v[1] = $t[1];
$v[2] = $t[2];
$z = '1';
}
$uu = $u[0];
$vv = $u[1];
return bccomp($vv, 0) == -1
? bcadd($vv, $calcbit)
: $vv;
}
/**
* Load Keys
*
* This function loads a keypair belonging to an external entity and your keypair object.
*
* @access public
* @param array(integer, integer)
* @param array(integer, integer)
* @return void
*/
public function load($yours, $theirs = false) {
if(!is_object($theirs) || !is_object($yours)) {
return false;
}
// Set your keypair.
if(
isset($yours->public)
&& isset($yours->private)
&& isset($yours->modulus)
) {
$this->yours = $this->key(
$yours->public,
$yours->private,
$yours->modulus
);
}
// Set their keypair. Normally, you won't have their private key.
if(
isset($theirs->public)
&& isset($theirs->modulus)
) {
$this->theirs = $this->key(
$theirs->public,
isset($theirs->private) ? $theirs->private : false,
$theirs->modulus
);
}
}
/**
* Create Key Object
*
* Takes the public, private and modulus and returns a key object with those values to be used in other methods
* of this library.
*
* @access public
* @param string $public
* @param string $private
* @param string $modulus
* @return object|false
*/
public function key($public, $private, $modulus) {
if(
!is_string($public)
|| !(is_string($private) || !$private)
|| !is_string($modulus)
|| !preg_match('/^[0-9]+$/', $public)
|| !(preg_match('/^[0-9]+$/', $private) || !$private)
|| !preg_match('/^[0-9]+$/', $modulus)
) {
return false;
}
$key = array(
'public' => $public,
'private' => $private ? $private : false,
'modulus' => $modulus,
);
return (object) $key;
}
/**
* Encrypt
*
* Encrypt a message with THEIR public key.
* If $theirs is false, then encrypt the message with YOUR private key (for digital signing).
*
* @access public
* @param string $message
* @param integer $step
* @return string
*/
public function encrypt($message, $theirs = true, $step = 3) {
if(
($theirs && (!isset($this->theirs->public) || !isset($this->theirs->modulus)))
||
(!$theirs && (!isset($this->yours->private) || !isset($this->yours->modulus)))
) {
return false;
}
$modulus = $theirs
? $this->theirs->modulus
: $this->yours->modulus;
$key = $theirs
? $this->theirs->public
: $this->yours->private;
$coded = '';
$max = strlen($message);
$packets = ceil($max / $step);
for($i = 0; $i < $packets; $i++) {
$packet = substr($message, $i * $step, $step);
$code = '0';
for($j = 0; $j < $step; $j++) {
$code = bcadd(
$code,
bcmul(
ord($packet[$j]),
bcpow('256', $j)
)
);
}
$code = bcpowmod($code, $key, $modulus);
$coded .= $code . ' ';
}
return trim($coded);
}
/**
* Decrypt
*
* Decrypt a message with YOUR private key.
* If $yours is false, then decrypt the message with THEIR public key (for digital signature authentication).
*
* @access public
* @param string $message
* @return string
*/
public function decrypt($message, $yours = true) {
if(
($yours && (!isset($this->yours->modulus) || !isset($this->yours->private)))
||
(!$yours && (!isset($this->theirs->public) || !isset($this->theirs->modulus)))
) {
return false;
}
$modulus = $yours
? $this->yours->modulus
: $this->theirs->modulus;
$key = $yours
? $this->yours->private
: $this->theirs->public;
$coded = explode(' ', $message);
$message = '';
foreach($coded as $code) {
$code = bcpowmod($code, $this->yours->private, $this->yours->modulus);
while(bccomp($code, '0') != 0) {
$ascii = bcmod($code, '256');
$code = bcdiv($code, '256', 0);
$message .= chr($ascii);
}
}
return $message;
}
/**
* Sign Message
*
* Sign a message's digest with your private key, to authenticate that you are the owner of the original message.
*
* @access public
* @param string $message
* @return string
*/
public function sign($message) {
return $this->encrypt(md5($message));
}
/**
* Prove Message
*
* Authenticate that a message's signature came from the owner of the private key, corresponding to the public
* you possess.
*
* @access public
* @param string $message
* @param string $signature
* @return boolean
*/
public function prove($message, $signature) {
// Decrypt the signature with their public key.
$digest = $this->decrypt($signature, false);
return $digest == md5($message);
}
}