[2847] | 1 | <?php |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | class rsa{ |
---|
| 5 | |
---|
| 6 | var $e; |
---|
| 7 | var $d; |
---|
| 8 | var $n; |
---|
| 9 | |
---|
| 10 | function rsa(){ |
---|
| 11 | session_start(); |
---|
| 12 | if (!isset($_SESSION['phpgw_session']['publ_key'])) |
---|
| 13 | { |
---|
| 14 | $p=$this->genPrim(); |
---|
| 15 | $q=$this->genPrim(); |
---|
| 16 | $this->n=bcmul($p,$q); |
---|
| 17 | $z=bcmul(bcsub($p,1),bcsub($q,1)); |
---|
| 18 | $this->d=$this->calc_d($z); |
---|
| 19 | $this->e=$this->calc_e($this->d,$z); |
---|
| 20 | $_SESSION['phpgw_session']['publ_key'] = $this->d; |
---|
| 21 | $_SESSION['phpgw_session']['priv_key'] = $this->e; |
---|
| 22 | $_SESSION['phpgw_session']['modulus'] = $this->n; |
---|
| 23 | } |
---|
| 24 | else |
---|
| 25 | { |
---|
| 26 | $this->e = $_SESSION['phpgw_session']['publ_key']; |
---|
| 27 | $this->d = $_SESSION['phpgw_session']['priv_key']; |
---|
| 28 | $this->n = $_SESSION['phpgw_session']['modulus'] ; |
---|
| 29 | } |
---|
| 30 | session_write_close(); |
---|
| 31 | } |
---|
| 32 | function encode($string){ |
---|
| 33 | $result = ""; |
---|
| 34 | for($i=0; $i < strlen($string); $i+=3) |
---|
| 35 | $result .= $this->encode_(ord($string[$i]).ord($string[$i+1]).ord($string[$i+2]),$this->e,$this->n)." "; |
---|
| 36 | return $result; |
---|
| 37 | } |
---|
| 38 | function decode($string){ |
---|
| 39 | $nums = split(" ",$string); |
---|
| 40 | foreach($nums as $todec) |
---|
| 41 | if (ord($todec) > 1){ |
---|
| 42 | $num = $this->decode_($todec,$this->d,$this->n); |
---|
| 43 | $num1 = substr($num,0,3); |
---|
| 44 | $num2 = substr($num,3,3); |
---|
| 45 | $num3 = substr($num,6,3); |
---|
| 46 | ($num1 > 1) && $result .= chr($num1); |
---|
| 47 | ($num2 > 1) && $result .= chr($num2); |
---|
| 48 | ($num3 > 1) && $result .= chr($num3); |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | return $result; |
---|
| 52 | } |
---|
| 53 | function get_publKey(){ |
---|
| 54 | return $_SESSION['phpgw_session']['publ_key']; |
---|
| 55 | } |
---|
| 56 | function get_mod(){ |
---|
| 57 | return $_SESSION['phpgw_session']['modulus']; |
---|
| 58 | } |
---|
| 59 | private function isPrime($Num){ |
---|
| 60 | $result = true; |
---|
| 61 | if($Num == 2 || $Num == 3) |
---|
| 62 | return true; |
---|
| 63 | if($Num % 2 == 0) |
---|
| 64 | return false; |
---|
| 65 | for($Divisor = 3; $Divisor < (sqrt($Num)+1); $Divisor+=2) |
---|
| 66 | { |
---|
| 67 | $Res = $Num % $Divisor; |
---|
| 68 | if($Res == 0) |
---|
| 69 | { |
---|
| 70 | $result=false; |
---|
| 71 | break; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | return $result; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | private function genPrim(){ |
---|
| 78 | $num=rand(100000,5000000); |
---|
| 79 | $i=$num; |
---|
| 80 | while($i++) |
---|
| 81 | if ($this->isPrime($i)) |
---|
| 82 | return $i; |
---|
| 83 | } |
---|
| 84 | private function GCD($e,$m) { |
---|
| 85 | $y = $e; |
---|
| 86 | $x = $m; |
---|
| 87 | |
---|
| 88 | while (bccomp($y, 0) != 0) { |
---|
| 89 | // modulus function |
---|
| 90 | $w = bcsub($x, bcmul($y, bcdiv($x, $y, 0)));; |
---|
| 91 | $x = $y; |
---|
| 92 | $y = $w; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | return $x; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | // |
---|
| 99 | // Calculating E under conditions: |
---|
| 100 | // GCD(N,E) = 1 and 1<E<N |
---|
| 101 | // |
---|
| 102 | private function calc_d($m){ |
---|
| 103 | $e = '3'; |
---|
| 104 | if(bccomp($this->GCD($e, $m), '1') != 0){ |
---|
| 105 | $e = '5'; |
---|
| 106 | $step = '2'; |
---|
| 107 | |
---|
| 108 | while(bccomp($this->GCD($e, $m), '1') != 0){ |
---|
| 109 | $e = bcadd($e, $step); |
---|
| 110 | |
---|
| 111 | if($step == '2'){ |
---|
| 112 | $step = '4'; |
---|
| 113 | }else{ |
---|
| 114 | $step = '2'; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | return $e; |
---|
| 119 | } |
---|
| 120 | private function calc_e ($Ee,$Em) { |
---|
| 121 | $u1 = '1'; |
---|
| 122 | $u2 = '0'; |
---|
| 123 | $u3 = $Em; |
---|
| 124 | $v1 = '0'; |
---|
| 125 | $v2 = '1'; |
---|
| 126 | $v3 = $Ee; |
---|
| 127 | |
---|
| 128 | while (bccomp($v3, 0) != 0) { |
---|
| 129 | $qq = bcdiv($u3, $v3, 0); |
---|
| 130 | $t1 = bcsub($u1, bcmul($qq, $v1)); |
---|
| 131 | $t2 = bcsub($u2, bcmul($qq, $v2)); |
---|
| 132 | $t3 = bcsub($u3, bcmul($qq, $v3)); |
---|
| 133 | $u1 = $v1; |
---|
| 134 | $u2 = $v2; |
---|
| 135 | $u3 = $v3; |
---|
| 136 | $v1 = $t1; |
---|
| 137 | $v2 = $t2; |
---|
| 138 | $v3 = $t3; |
---|
| 139 | $z = '1'; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | $uu = $u1; |
---|
| 143 | $vv = $u2; |
---|
| 144 | |
---|
| 145 | if (bccomp($vv, 0) == -1) { |
---|
| 146 | $inverse = bcadd($vv, $Em); |
---|
| 147 | } else { |
---|
| 148 | $inverse = $vv; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return $inverse; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | private function encode_($val,$e,$n){ |
---|
| 156 | return bcpowmod($val,$e,$n); |
---|
| 157 | } |
---|
| 158 | private function decode_($val,$d,$n){ |
---|
| 159 | return bcpowmod($val,$d,$n); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | ?> |
---|
| 165 | |
---|