1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
rsabcmath.php
См. документацию.
1<?
3{
4 public function LoadKeys()
5 {
6 $arKeys = unserialize(COption::GetOptionString("main", "~rsa_keys_bcmath", ""), ['allowed_classes' => false]);
7 if(!is_array($arKeys))
8 return false;
9 return $arKeys;
10 }
11
12 public function SaveKeys($arKeys)
13 {
14 COption::SetOptionString("main", "~rsa_keys_bcmath", serialize($arKeys));
15 }
16
17 public function Decrypt($data)
18 {
19 $d = self::raw2int(base64_decode($this->_D));
20 $n = self::raw2int(base64_decode($this->_M));
21
22 $out = '';
23 $blocks = explode(' ', $data);
24 foreach($blocks as $block)
25 {
26 $block = self::powmod(self::raw2int(base64_decode($block)), $d, $n);
27 while(bccomp($block, '0') != 0)
28 {
29 $x = bcmod($block, '256');
30 $block = bcdiv($block, '256', 0);
31 $out .= chr($x);
32 }
33 }
34 return $out;
35 }
36
37 public function Keygen($keylen=false)
38 {
39 if($keylen === false)
40 $keylen = 512;
41 else
42 $keylen = intval($keylen);
43
44 $pl = intval(($keylen + 1) / 2);
45 $ql = $keylen - $pl;
46
47 $p = self::getRndPrime($pl);
48 $q = self::getRndPrime($ql);
49
50 $x = self::getkeysfrompq($p, $q, 1) ;
51
52 return array(
53 "M" => base64_encode(self::int2raw($x[0])),
54 "E" => base64_encode(self::int2raw($x[1])),
55 "D" => base64_encode(self::int2raw($x[2])),
56 "chunk" => $keylen/8,
57 );
58 }
59
60 private static function getRndPrime($cnt)
61 {
62 $btn = intval($cnt / 8);
63 $bn = $cnt % 8;
64 $ret = '0';
65
66 while(self::bitlenght($ret) != $cnt)
67 {
68 $str = '';
69 for($i = 0; $i < $btn; $i++)
70 $str .= chr(rand() & 0xff);
71
72 $n = rand() & 0xff;
73 $n |= 0x80;
74 $n >>= 8 - $bn;
75 $str .= chr($n);
76 $ret = self::raw2int($str);
77
78 if(!bccomp(bcmod($ret, '2'), '0'))
79 $ret = bcadd($ret, '1');
80
81 while(!self::is_prime($ret))
82 $ret = bcadd($ret, '2');
83 }
84 return $ret;
85 }
86
87 private static function bitlenght($in)
88 {
89 $t = self::int2raw($in);
90 $out = mb_strlen($t) * 8;
91
92 $t = ord($t[mb_strlen($t) - 1]);
93
94 if(!$t)
95 {
96 $out -= 8;
97 }
98 else
99 {
100 while(!($t & 0x80))
101 {
102 $out--;
103 $t <<= 1;
104 }
105 }
106 return $out;
107 }
108
109 private static function is_prime($in)
110 {
111 static $ps = null;
112 static $psc = 0;
113 if(is_null($ps))
114 {
115 $ps = array();
116 for($i = 0; $i < 10000; $i++)
117 $ps[] = $i;
118 $ps[0] = $ps[1] = 0;
119 for($i = 2; $i < 100; $i++)
120 {
121 while(!$ps[$i])
122 $i++;
123 $j = $i;
124 for($j += $i; $j < 10000; $j += $i)
125 $ps[$j] = 0;
126 }
127 $j = 0;
128 for($i = 0; $i < 10000; $i++)
129 {
130 if($ps[$i]) $ps[$j++] = $ps[$i];
131 }
132 $psc = $j;
133 }
134
135 for($i = 0; $i < $psc; $i++)
136 {
137 if(bccomp($in, $ps[$i]) <= 0)
138 return true;
139 if(!bccomp(bcmod($in, $ps[$i]), '0'))
140 return false;
141 }
142
143 for($i = 0; $i < 7; $i++)
144 {
145 if(!self::miller($in, $ps[$i]))
146 return false;
147 }
148 return true;
149 }
150
151 private static function getkeysfrompq($p, $q)
152 {
153 $n = bcmul($p, $q);
154 $m = bcmul(bcsub($p, 1), bcsub($q, 1));
155 $e = self::get_e($m);
156 $d = self::ext($e, $m);
157 return array($n, $e, $d);
158 }
159
160 private static function ext($e1, $em)
161 {
162 $u1 = '1';
163 $u2 = '0';
164 $u3 = $em;
165 $v1 = '0';
166 $v2 = '1';
167 $v3 = $e1;
168
169 while(bccomp($v3, 0) != 0)
170 {
171 $qt = bcdiv($u3, $v3, 0);
172 $t1 = bcsub($u1, bcmul($qt, $v1));
173 $t2 = bcsub($u2, bcmul($qt, $v2));
174 $t3 = bcsub($u3, bcmul($qt, $v3));
175 $u1 = $v1;
176 $u2 = $v2;
177 $u3 = $v3;
178 $v1 = $t1;
179 $v2 = $t2;
180 $v3 = $t3;
181 $z = '1';
182 }
183
184 $uu = $u1;
185 $vv = $u2;
186
187 if(bccomp($vv, 0) == -1)
188 $ret = bcadd($vv, $em);
189 else
190 $ret = $vv;
191
192 return $ret;
193 }
194
195 private static function get_e($m)
196 {
197 $et = '257';
198 if(bccomp(self::GCD($et, $m), '1') != 0)
199 {
200 $et = '5';
201 $c = '2';
202
203 while(bccomp(self::GCD($et, $m), '1') != 0)
204 {
205 $et = bcadd($et, $c);
206 if($c == '2')
207 $c = '4';
208 else
209 $c = '2';
210 }
211 }
212
213 return $et;
214 }
215
216 private static function GCD($e, $m)
217 {
218 $e1 = $e;
219 $m1 = $m;
220 while(bccomp($e1, 0) != 0)
221 {
222 $w = bcsub($m1, bcmul($e1, bcdiv($m1, $e1, 0)));;
223 $m1 = $e1;
224 $e1 = $w;
225 }
226
227 return $m1;
228 }
229
230 private static function int2raw($in)
231 {
232 $out = '';
233
234 if($in=='0')
235 return chr(0);
236
237 while(bccomp($in, '0'))
238 {
239 $out .= chr(bcmod($in, '256'));
240 $in = bcdiv($in, '256');
241 }
242 return $out;
243 }
244
245 private static function raw2int($in)
246 {
247 $out = '0';
248 $n = mb_strlen($in);
249 while($n > 0)
250 {
251 $out = bcadd(bcmul($out, '256'), ord($in[--$n]));
252 }
253 return $out;
254 }
255
256 private static function powmod($n, $p, $m)
257 {
258 if(function_exists('bcpowmod'))
259 return bcpowmod($n, $p, $m);
260
261 $out = '1';
262 do
263 {
264 if(!bccomp(bcmod($p, '2'), '1'))
265 {
266 $out = bcmod(bcmul($out, $n), $m);
267 }
268 $n = bcmod(bcpow($n, '2'), $m);
269 $p = bcdiv($p, '2');
270 }
271 while(bccomp($p, '0'));
272
273 return $out;
274 }
275
276 private static function miller($in, $b)
277 {
278 if(!bccomp($in, '1'))
279 return false;
280
281 $t = bcsub($in, '1');
282
283 $nulcnt = 0;
284 while(!bccomp(bcmod($t, '2'), '0'))
285 {
286 $nulcnt++;
287 $t = bcdiv($t, '2');
288 }
289
290 $t = self::powmod($b, $t, $in);
291 if(!bccomp($t, '1'))
292 return true;
293
294 while($nulcnt--)
295 {
296 if(!bccomp(bcadd($t, '1'), $in))
297 return true;
298
299 $t = self::powmod($t, '2', $in);
300 }
301 return false;
302 }
303}
304?>
Определения rsabcmath.php:3
Decrypt($data)
Определения rsabcmath.php:17
LoadKeys()
Определения rsabcmath.php:4
Keygen($keylen=false)
Определения rsabcmath.php:37
SaveKeys($arKeys)
Определения rsabcmath.php:12
Определения rsasecurity.php:6
$str
Определения commerceml2.php:63
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$p
Определения group_list_element_edit.php:23
$z
Определения options.php:31
$i
Определения factura.php:643
$n
Определения update_log.php:107