/** returns RSA key pair with length $bit_len */ function generate_key_pair($bit_len) { // generate two primes p and q $p_len = (int) ($bit_len / 2) + 1; $q_len = $bit_len - $p_len; $p = get_prime($p_len); $q = get_prime($q_len); // $n - is shared modulus $n = bi_mul($p, $q); // generate public ($e) and private ($d) keys $pq = bi_mul(bi_dec($p), bi_dec($q)); do { $e = bi_rand($q_len, 'microtime_generator'); } while (!bi_is_zero(bi_dec(bi_gcd($e, $pq)))); $d = bi_invmod($e, $pq); return array('n' => bi_serialize($n), 'public_key' => bi_serialize($e), 'private_key' => bi_serialize($d)); }
/** * Calculates $num - 1 * * @param big_int resource $num * @return big_int resource * @access public */ function dec($num) { return bi_dec($num); }
echo '$a + $b = [', bi_to_str($c), "]<br/>\n"; $c = bi_sub($a, $b); echo '$a - $b = [', bi_to_str($c), "]<br/>\n"; $c = bi_mul($a, $b); echo '$a * $b = [', bi_to_str($c), "]<br/>\n"; $c = bi_div($a, $b); echo '$a / $b = [', bi_to_str($c), "]<br/>\n"; $c = bi_mod($a, $b); echo '$a % $b = [', bi_to_str($c), "]<br/>\n"; $c = bi_abs($a); echo 'abs($a) = [', bi_to_str($c), "]<br/>\n"; $c = bi_neg($a); echo 'neg($a) = [', bi_to_str($c), "]<br/>\n"; $c = bi_inc($a); echo 'inc($a) = [', bi_to_str($c), "]<br/>\n"; $c = bi_dec($a); echo 'dec($a) = [', bi_to_str($c), "]<br/>\n"; $c = bi_sqr($a); echo 'sqr($a) = [', bi_to_str($c), "]<br/>\n"; echo 'cmp($a, $b) = ', bi_cmp($a, $b), "<br/>\n"; echo 'cmp_abs($a, $b) = ', bi_cmp_abs($a, $b), "<br/>\n"; echo 'is_zero($a) = ', bi_is_zero($a) ? 'true' : 'false', "<br/>\n"; echo 'is_one($a) = ', bi_is_one($a) ? 'true' : 'false', "<br/>\n"; echo 'sign($a) = ', bi_sign($a), "<br/>\n"; /* bitset functions */ echo '<h3>bitset functions</h3>' . "\n"; echo '<div style="text-align:right">'; echo '$a = [', bi_to_str($a, 2), "]<br/>\n"; echo '$b = [', bi_to_str($b, 2), "]<br/>\n";