Algoritma Kriptografi RSA (Rivest/Shamir/Adelman)
Oleh: Djoko Tri W (18109011) Sebagai tugas mata kuliah Sistem Keamanan Komputer
Source: - tugas-rsa.php
Algoritma RSA ?>
2
- tugas-rsa-func.php * * This script is distributed under the terms of the GNU General Public License (GPL) * See http://www.gnu.org/licenses/gpl.txt for license details * * Note: Hanya bisa dijalankan di php5 (function math bcpowmod() tidak ada di php4) * */ function cek_prima($bil) { if ($bil < 2) { return FALSE; } // nol (0) dan 1 bukan prima for ($i=2; $i<=($bil / 2); $i++) { if($bil % $i == 0) { return FALSE; } } return TRUE; } function gcd($e, $totient) { $y = $e; $x = $totient; while(bccomp($y, 0) != 0) { $w = $x % $y; $x = $y; $y = $w; } return $x; } /* * Cari nilai D, * D = E-1 (mod N) */ function cari_d($Ee, $Em) { $u1 = 1; $u2 = 0; $u3 = $Em; $v1 = 0; $v2 = 1; $v3 = $Ee; while ($v3 $qq $t1 $t2 $t3 $u1 $u2 $u3 $v1 $v2 $v3 $z }
!= 0) { = floor($u3 / $v3); = $u1 - ($qq * $v1); = $u2 - ($qq * $v2); = $u3 - ($qq * $v3); = $v1; = $v2; = $v3; = $t1; = $t2; = $t3; = 1;
$uu = $u1;
3
$vv = $u2; if ($vv < 0) { $inverse = $vv + $Em; } else { $inverse = $vv; } return $inverse; } /* * Cari nilai E, * GCD(N,E) = 1 and 1<E
4
$jmlchar = count($char); for($i=0; $i<$jmlchar; $i++) { // karakter = karakter enkripsi ^ D (mod N) $code = bcpowmod($char[$i], $d, $n); while(bccomp($code, "0") != 0) { // kembalikan nilai ASCII (mod 256 dari karakter div 256) ke karakter asli $ascii = $code % 256; $code = floor($code / 256); $isiteks .= chr($ascii); } } return $isiteks; } ?>
Output:
5
6