/** 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 $num1 * $num2 * * @param big_int resource $num1 * @param big_int resource $num2 * @return big_int resource * @access public */ function mul($num1, $num2) { return bi_mul($num1, $num2); }
function bcmul($a, $b) { return bi_to_str(bi_mul($a, $b)); }
/* string bi_base_convert(string $num, int $base_from, int $base_to) Converts string representation of $num from base $base_from to $base_to */ $num = '-12345678900987654321'; echo 'Decimal number [', $num, '] equals to [', bi_base_convert($num, 10, 36), '] by base 36', "<br/>\n"; /* basic functions */ echo '<h3>basic functions</h3>' . "\n"; $c = bi_add($a, $b); 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";
/** * Multiply two arbitrary precision number * * @param string $a The left operand, as a string. * @param string $b The right operand, as a string. * @access public * @return string Returns the result as a string. */ public function mul($a, $b) { return bi_mul($a, $b); }