termasuk ("PublicKey. php"); include ("PrivateKey. php"); include ("NumberTheory. php"); termasuk ("tanda tangan. php"); $pesan = $_POST["pesan"]; $_p = '6277101735386680763835789423207666416083908700390324961279'; $_b = bchexdec('0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1'); $p192 = Poin baru($curve_192, $_Gx, $_Gy, $_r); (2)$pubk = new PublicKey($p192, Point. rmul($p192, $d)); $sig = $privk->tanda($e, $k); $s = $sig->getS(); $tanda tangan = "(". $s. "),(". $r. ")"; } elseif (isset($_POST['kirim'])) { echo $_POST['subjek']; } lain{ ?> CurveFp. php kelas CurveFp{ dilindungi $a = 0; fungsi publik __construct($prime, $a, $b) { $this->a = $a; $ini->b = $b; $ini->prime = $prime; fungsi publik berisi($x, $y) { $eq_zero = null; $eq_zero = bccomp(bcmod(bcsub(bcpow($y, 2), bcadd(bcadd(bcpow($x, 3), bcmul($this->a, $x)), $this->b)), $ini->utama), 0); (5)kembali benar; kembali salah; } fungsi publik getA() { return $this->a; } fungsi publik getB() { return $this->b; } fungsi publik getPrime() { return $this->prime; } fungsi statis publik cmp(CurveFp $cp1, CurveFp $cp2) { $same = null; jika (bccomp($cp1->a, $cp2->a) == 0 && bccomp($cp1->b, $cp2->b) == 0 && bccomp($cp1->prime, $cp2-> prima) == 0) { kembali 0; kembali 1; Poin. php (6) define('MAX_BASE', 128); publik $kurva; public static $tak terhingga = 'tak terhingga'; fungsi publik __construct(CurveFp $curve, $x, $y, $order = null) { $this->curve = $curve; $ini->x = $x; $ini->pesanan = $pesanan; if (isset($this->curve) && ($this->curve instanceof CurveFp)) { if (. $this->curve->contains($this->x, $this->y)) { lempar ErrorException baru ("Kurva". print_r($ini->kurva, benar). " tidak memiliki titik (". $x. " , ". $y. " )"); } jika ($this->order. = nol) { jika (diri. cmp(diri. mul($order, $this), self. $tak terhingga). = 0) { lempar ErrorException baru (""); } } } } fungsi statis publik cmp($p1, $p2) { jika (. ($p1 instanceof Point)) { if (($p2 instanceof Point)) mengembalikan 1; jika (. ($p2 instanceof Point)) mengembalikan 0; } (7)kembali 1; jika (. ($p1 instanceof Point)) mengembalikan 0; } jika (bccomp($p1->x, $p2->x) == 0 && bccomp($p1->y, $p2->y) == 0 && CurveFp. cmp($p1->kurva, $p2->kurva)) { kembali 0; kembali 1; } fungsi statis publik add($p1, $p2) { jika (diri. cmp($p2, mandiri. $infinity) == 0 && ($p1 instanceof Point)) { return $p1; } jika (diri. cmp($p1, mandiri. $infinity) == 0 && ($p2 instanceof Point)) { return $p2; } jika (diri. cmp($p1, mandiri. $tak terhingga) == 0 && diri. cmp($p2, mandiri. $tak terhingga) == 0) { mengembalikan diri. $tak terhingga; } jika (KurvaFp. cmp($p1->kurva, $p2->kurva) == 0) { jika (bcmod(bccomp($p1->x, $p2->x), $p1->curve->getPrime()) == 0) { jika (bcmod(bcadd($p1->y, $p2- >y), $p1->curve->getPrime()) == 0) { mengembalikan diri. $tak terhingga; } kalau tidak { mengembalikan diri. ganda($p1); } (8)$l = bcmod(bcmul(bcsub($p2->y, $p1->y), mandiri. inverse_mod(bcsub($p2->x, $p1->x), $p)), $p); $x3 = bcmod(bcsub(bcsub(bcpow($l, 2), $p1->x), $p2->x), $p); $langkah1 = bcmul($l, $langkah0); $y3 = bcmod(bcsub(bcmul($l, bcsub($p1->x, $x3)), $p1->y), $p); $y3 = bcadd($p, $y3); $p3 = Poin baru($p1->kurva, $x3, $y3); }kalau tidak { throw new ErrorException("Kurva tidak pas. "); } } fungsi statis publik mul($x2, Point $p1) { $e = $x2; jika (diri. cmp($p1, mandiri. $tak terhingga) == 0) { mengembalikan diri. $tak terhingga; } jika ($p1->pesanan. = null) { $e = bcmod($e, $p1->pesanan); if (bcomp($e, 0) == 0) { mengembalikan diri. $tak terhingga; if (bccomp($e, 0) == 1) { $e3 = bcmul(3, $e); $negatif_diri = poin baru($p1->kurva, $p1->x, bcsub(0, $p1->y), $p1->urutan); . bit_terkiri($e3), 2); (9)while (bccomp($i, 1) == 1) { $result = self. ganda($hasil); $e3bit = bcomp(self. bcand($e3, $i), '0'); . bcand($e, $i), '0'); . = 0 && $ebit == 0) { $hasil = diri sendiri. tambahkan($hasil, $p1); . = 0) { $hasil = diri sendiri. add($hasil, $negatif_diri); $i = bcdiv($i, 2); return $hasil; } fungsi statis publik leftmost_bit($x) { if (bccomp($x, 0) == 1) { $hasil = 1; sementara (bccomp($hasil, $x) == -1. bcomp($hasil, $x) == 0) { $hasil = bcmul(2, $hasil); } return bcdiv($hasil, 2); } fungsi statis publik rmul(Point $x1, $m) { kembali sendiri. mul($m, $x1); } fungsi publik __toString() { jika (. ($this instanceof Point) && $this == self. $infinity) mengembalikan diri. $tak terhingga; kembali "(". $ini->x. ",". $ini->y. ")"; (10)$a = $p1->kurva->getA(); $invers = diri sendiri. inverse_mod(bcmul(2, $p1->y), $p); $l = bcmod(bcmul(bcadd($tiga_x2, $a), $invers), $p); $y3 = bcmod(bcsub(bcmul($l, bcsub($p1->x, $x3)), $p1->y), $p); $y3 = bcadd($p, $y3); $p3 = Poin baru($p1->kurva, $x3, $y3); } fungsi statis publik inverse_mod($a, $m) { while (bccomp($a, 0) == -1) { $a = bcadd($m, $a); while (bccomp($m, $a) == -1) { $a = bcmod($a, $m); } $c = $a; sementara (bcomp($c, 0). = 0) { $temp1 = $c; $q = bcdiv($d, $c, 0); (11)$ud = $temp2; $hasil = ''; jika (bccomp($d, 1) == 0) { jika (bccomp($ud, 0) == 1) $result = $ud; sebaliknya $hasil = bcadd($ud, $m); { lempar ErrorException baru ("ERROR. $a dan $m tidak relatif prima. "); } return $hasil; fungsi statis publik bcand($x, $y) { mengembalikan diri. _bcbitwise_internal($x, $y, 'self. _bcand'); fungsi statis publik _bcand($x, $y) { return $x & $y; } fungsi statis publik _bcbitwise_internal($x, $y, $op) { $bx = self. bc2bin($x); $oleh = diri sendiri. bc2bin($y); . equalbinpad($bx, $by); $ret = ''; untuk ($ix = 0; $ix < strlen($bx); $ix++) { $xd = substr($bx, $ix, 1); $yd = substr($oleh, $ix, 1); $ret. = call_user_func($op, $xd, $yd); (12)} fungsi statis publik bin2bc($num) { kembali sendiri. base2dec($num, MAX_BASE); fungsi statis publik bc2bin($num) { kembali sendiri. dec2base($num, MAX_BASE); fungsi statis publik dec2base($dec, $base, $digits=FALSE) { if ($base < 2 atau $base > 256) die("Basis Tidak Valid. ". $dasar); $nilai = ""; . $digit) $digit = diri sendiri. angka($basis); . $nilai; $nilai = $digit[intval($dec)]. $nilai; } fungsi statis publik base2dec($nilai, $base, $digits=FALSE) { if ($base < 2 atau $base > 256) die("Basis Tidak Valid. ". $dasar); jika ($basis < 37) $nilai = strtolower($nilai); . $digit) $digit = diri sendiri. angka($basis); (13)$elemen = strpos($digit, $nilai[$loop]); return (string) $dec; public static function digit($base) { if ($base > 64) { $digit = ""; for ($loop = 0; $loop < 256; $loop++) { $digit. =chr($ulang); } } kalau tidak { $digit = "0123456789abcdefghijklmnopqrstuvwxyz"; . ="ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; $digit = substr($digit, 0, $basis); } fungsi statis publik equalbinpad(&$x, &$y) { $xlen = strlen($x); $ylen = strlen($y); $panjang = maks($xlen, $ylen); . fixedbinpad($x, $panjang); . fixedbinpad($y, $panjang); fungsi statis publik fixedbinpad(&$num, $panjang) { $pad = ''; untuk ($ii = 0; $ii < $panjang - strlen($num); $ii++) { $pad. = diri. bc2bin('0'); } (14)} fungsi publik getX() { return $this->x; } fungsi publik getY() { return $this->y; } fungsi publik getCurve() { return $this->curve; } fungsi publik getOrder() { return $this->order; } } ?> PublicKey. php kelas PublicKey{ dilindungi $curve; public function __construct(Point $generator, Point $point) { $this->curve = $generator->getCurve(); $ini->generator = $generator; $n = $generator->getOrder(); jika ($n == nol) { lempar ErrorException baru (""); (15)} jika (bcomp($point->getX(), 0) == -1. bcomp($n, $point->getX()). = 1. bcomp($point->getY(), 0) == -1. bcomp($n, $point->getY()). = 1) { lempar ErrorException baru (""); } verifikasi fungsi publik($hash, Tanda Tangan $signature) { $G = $this->generator; $n = $this->generator->getOrder(); $r = $tanda tangan->getR(); jika (bcomp($r, 1) == -1. bccomp($r, bcsub($n, 1)) == 1) { return false; } jika (bccomp($s, 1) == -1. bcomp($s, bcsub($n, 1)) == 1) { return false; } $c = Teori Bilangan. mode_terbalik($s, $n); $xy = Poin. tambahkan (titik. mul($u1, $G), Poin. mul($u2, $poin)); if (bcomp($v, $r) == 0) return true; lain { kembali salah; } fungsi publik getCurve() { return $this->curve; } (16)return $this->generator; fungsi publik getN() { return $this->an; } fungsi publik getPoint() { return $this->point; } fungsi publik getPublicKey() { return $this; } } ?> PrivateKey. php kelas PrivateKey{ private $public_key; fungsi publik __construct(PublicKey $public_key, $secret_multiplier) { $this->public_key = $public_key; $this->secret_multiplier = $secret_multiplier; tanda fungsi publik($hash, $random_k) { $G = $this->public_key->getGenerator(); $k = bcmod($random_k, $n); . mul($k, $G); jika (bcomp($r, 0) == 0) { (17)$s = bcmod(bcmul(NumberTheory. inverse_mod($k, $n), bcmod(bcadd($hash, bcmul($this->secret_multiplier, $r)), $n)), $n); jika (bcomp($s, 0) == 0) { throw new ErrorExcpetion("error: Nilai S = 0 return new Signature($r, $s); fungsi statis publik int_to_string($x) { jika (bccomp($x, 0). = -1) { if (bcomp($x, 0) == 0) return chr(0); $hasil = ""; while (bcomp($x, 0) == 1) { $q = bcdiv($x, 256, 0); $hasil = $ascii. $hasil; } return $hasil; } fungsi statis publik string_to_int($s) { $result = 0; untuk ($c = 0; $c < strlen($s); $c++) { $hasil = bcadd(bcmul(256, $hasil), ord($s[$c])); return $hasil; fungsi statis publik digest_integer($m) { mengembalikan diri. string_to_int(hash('sha1', self. int_to_string($m), benar)); (18)$n = $generator->getOrder(); jika (bccomp($x, 0) == -1. bcomp($n, $x). = 1. bcomp($y, 0) == -1. bcomp($n, $y). = 1) { kembali salah; $berisi = $curve->berisi($x, $y); . $penahanan) { kembali salah; $titik = titik baru($kurva, $x, $y); . mul($n, $titik); jika (. (Titik. cmp($op, Titik. $tak terhingga) == 0)) { return false; } kembali benar; } ?> Teori Angka. php kelas Teori Bilangan { public static function modular_exp($base, $exponent, $modulus) { if ($exponent < 0) { return new ErrorException("Eksponen negatif (". $eksponen. ") tidak diperbolehkan"); $p = bcpowmod($basis, $eksponen, $modulus); } } (19)if (end($polymod) == 1 && count($polymod) > 1) { while (count($poly) >= count($polymod)) { jika (end($poli). = 0) { for ($i = 2; $i < count($polymod) + 1; $i++) { $poli[hitung($poli) - $i] = bcmod(bcsub($poli[hitung($poli) - $i], bcmul(end($poli), $polimod[hitung($polimod) - $i] )), $p); $poli = array_slice($poli, 0, hitung($poli) - 2); } } return $poli; } fungsi statis publik polynomial_multiply_mod($m1, $m2, $polymod, $p) { $prod = array(); untuk ($i = 0; $i < hitung($m1); $i++) { untuk ($j = 0; $j < hitung($m2); $j++) { $indeks = $i + $j; $prod[$indeks] = bcmod((bcadd($prod[$indeks], bcmul($m1[$i], $m2[$j]))), $p); } mengembalikan diri. polynomial_reduce_mod($prod, $polymod, $p); fungsi statis publik polynomial_exp_mod($base, $exponent, $polymod, $p) { $s = ''; if ($exponent < $p) { if ($exponent == 0) return 1; $G = $dasar; (20)$s = larik(1); $G = diri sendiri. polynomial_multiply_mod($G, $G, $polymod, $p); $s = diri sendiri. polynomial_multiply_mod($G, $s, $polymod, $p); } kembalikan $s; fungsi statis publik jacobi($a, $n) { if ($n >= 3 && $n % 2 == 1) { $a = bcmod($a, $n); jika ($a == 0) kembalikan 0; while (bcmod($a1, 2) == 0) { $a1 = bcdiv($a1, 2); $e = bcadd($e, 1); jika (bcmod($e, 2) == 0. bcmod($n, 8) == 1. bcmod($n, 8) == 7) $s = 1; selain itu $s = -1; jika (bcmod($n, 4) == 3 && bcmod($a1, 4) == 3) $s = -$s; (21)} } fungsi statis publik square_root_mod_prime($a, $p) { if (0 <= $a && $a < $p && 1 < $p) { jika ($a == 0) kembalikan 0; $jac = diri sendiri. jacob($a, $p); lempar SquareRootException($a. "tidak memiliki modulo akar kuadrat". $p); mengembalikan diri. modular_exp($a, bcdiv(bcadd($p, 1), 4), $p); $d = diri sendiri. modular_exp($a, bcdiv(bcsub($p, 1), 4), $p); mengembalikan diri. modular_exp($a, bcdiv(bcadd($p, 3), 8), $p); kembali (bcmod(bcmul(bcmul(2, $a), mandiri. modular_exp(bcmul(4, $a), bcdiv(bcsub($p, 5), 8), $p)), $p)); } untuk ($b = 2; $b < $p; $p++) { jika (diri. jacobi(bcmul($b, bcsub($b, bcmul(4, $a))), $p) == -1) { $f = array($a, -$b, 1); $ff = diri sendiri. polynomial_exp_mod(array(0, 1), bcdiv(bcadd($p, 1), 2), $f, $p); return $ff[0]; } } } (22)while (bcomp($a, 0) == -1) { $a = bcadd($m, $a); } while (bccomp($m, $a) == -1) { $a = bcmod($a, $m); } $c = $a; sementara (bcomp($c, 0). = 0) { $temp1 = $c; $q = bcdiv($d, $c, 0); $uc = bcsub($ud, bcmul($q, $uc)); $vd = $temp3; $hasil = ''; jika (bccomp($d, 1) == 0) { jika (bccomp($ud, 0) == 1) $result = $ud; sebaliknya $hasil = bcadd($ud, $m); (23)return $hasil; fungsi statis publik gcd2($a, $b) { while ($a) { $temp = $a; $a = bcmod($b, $a); } kembalikan $b; fungsi statis publik gcd($a) { if (count($a) > 1) kembali array_reduce($a, "self. gcd2", $a[0]); } fungsi statis publik lcm2($a, $b) { $ab = bcmul($a, $b); $g = diri sendiri. gcd2($a, $b); } fungsi statis publik lcm($a) { if (count($a) > 1) kembali array_reduce($a, "self. lcm2", $a[0]); } faktorisasi fungsi statis publik($n) { if (is_int($n). is_long($n)) { if ($n < 2) mengembalikan array(); mendahului (diri sendiri. $smallprimes as $d) { if ($d > $n) (24)$q = $n / $d; while ($d <= $n) { $n = $q; $q = $n / $d; . = 0) istirahat; array_push($hasil, larik($d, $hitung)); } if ($n > end(self. $smallprimes)) { if (is_prime($n)) { array_push($hasil, larik($n, 1)); $d = akhir(diri. $smallprimes); $d += 2; (25)istirahat; array_push($hasil, larik($n, 1)); } } } return $hasil; } fungsi statis publik phi($n) { jika (is_int($n). is_long($n)) { jika ($n < 3) kembali 1; $ff = diri sendiri. faktorisasi($n); $e = $f[1]; $hasil = bcmul($hasil, bcmul(bcpow($f[0], bcsub($e, 1)), bcsub($f[0], 1))); $hasil = bcmul($hasil, bcsub($f[0], 1)); } return $hasil; } fungsi statis publik carmichael($n) { mengembalikan diri. carmichael_of_factorized(self. faktorisasi($n)); (26)if (count($f_list) < 1) return 1; $hasil = diri sendiri. carmichael_of_ppower($f_list[0]); $hasil = lcm($hasil, mandiri. carmichael_of_ppower($f_list[$i])); return $hasil; fungsi statis publik carmichael_of_ppower($pp) { $p = $pp[0]; $a = $pp[1]; jika ($p == 2 && $a > 2) kembalikan 1 >> ($a - 2); return bcmul(($p - 1), bcpow($p, ($a - 1))); fungsi statis publik order_mod($x, $m) { if ($m <= 1) kembali 0; if (gcd($x, m) == 1) { $z = $x; $hasil = 1; . = 1) { $z = bcmod(bcmul($z, $x), $m); return $hasil; } fungsi statis publik terbesar_faktor_relatively_prime($a, $b) { while (true) { (27)istirahat; } kembalikan $a; fungsi statis publik kinda_order_mod($x, $m) { mengembalikan diri. order_mod($x, mandiri. faktor_terbesar_relatif_prime($m, $x)); fungsi statis publik is_prime($n) { self. $miller_rabin_test_count = 0; $k = 0; $m = bcsub($n, 1); while (bcmod($m, 2) == 0) { $k = bcadd($k, 1); $m = bcdiv($m, 2); untuk ($i = 0; $i < $t; $i++) { $a = bcmath_Utils. bcrand(1, bcsub($n, 1)); . modular_exp($a, $m, $n); . = 1 && $b0. = bcsub($n, 1)) { $j = 1; sementara ($j <= $k - 1 && $b0. = bcsub($n, 1)) { $b0 = diri. modular_exp($b0, 2, $n); jika ($b0 == 1) { (28)kembali salah; $j++; jika ($b0. = bcsub($n, 1)) { diri sendiri. $miller_rabin_test_count = $i + 1; } } } kembali benar; fungsi statis publik next_prime($starting_value) { if (bccomp($starting_value, 2) == -1) kembali 2; $hasil = bcmath_Utils. bcor(bcadd($starting_value, 1), 1); . diri sendiri. is_prime($result)) { $hasil = bcadd($hasil, 2); return $hasil; $miller_rabin_test_count statis publik; (29)577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1063, 107 , 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229); |