Beispiel #1
0
/**
    encrypts / decrypts $text with key ($e, $n)
*/
function encrypt_text($text, $e, $n)
{
    $tmp = bi_unserialize($text);
    $e = bi_unserialize($e);
    $n = bi_unserialize($n);
    if (bi_cmp($tmp, $n) >= 0) {
        die('$text is too long to encrypt by key with length ' . bi_bit_len($n) . ' bits' . "<br/>\n");
    }
    return bi_serialize(bi_powmod($tmp, $e, $n));
}
Beispiel #2
0
 /**
  * Transforms binary representation of large integer into its native form.
  * 
  * Example of transformation:
  *    $str = "\x12\x34\x56\x78\x90";
  *    $num = 0x9078563412;
  *
  * @param string $str
  * @return big_int resource
  * @access public
  */
 function bin2int($str)
 {
     return bi_unserialize($str);
 }
Beispiel #3
0
echo 'pow($a, 4) = [', bi_to_str($c), "]<br/>\n";
// argument of bi_fact() must have integer type (not BIG_INT)
$c = bi_fact(100);
echo '100! = [', bi_to_str($c), "]<br/>\n";
$c = bi_sqrt($a);
echo 'sqrt($a) = [', bi_to_str($c), "]<br/>\n";
$c = bi_sqrt_rem($a);
echo 'sqrt_rem($a) = [', bi_to_str($c), "]<br/>\n";
$c = bi_gcd($a, $b);
echo 'GCD($a, $b) = [', bi_to_str($c), "]<br/>\n";
// Miller-Rabin primality test of $a
echo 'miller_test($a, $b) = ', bi_miller_test($a, $b), "<br/>\n";
// primality test of $a
echo 'is_prime($a) = ', bi_is_prime($a), "<br/>\n";
// Jacobi symbol ($a|$b)
echo 'jacobi($a, $b) = ', bi_jacobi($a, $b), "<br/>\n";
$c = bi_div_extended($a, $b);
echo '$a = $b * ', bi_to_str($c[0]), ' + ', bi_to_str($c[1]), "<br/>\n";
$c = bi_gcd_extended($a, $b);
echo 'abs($a) * ', bi_to_str($c[1]), ' + abs($b) * ', bi_to_str($c[2]), ' = ', bi_to_str($c[0]), "<br/>\n";
// serialize $a into sequence of bytes
$str = bi_serialize($a);
echo '$str = serialize($a) = [', base64_encode($str), "]<br/>\n";
// unserialize $a from sequence of bytes
$a = bi_unserialize($str);
echo 'unserialize($str) = [', bi_to_str($a), "]<br/>\n";
// show package info
var_dump(bi_info());
echo 'the list of all functions in BIG_INT module:', "<br/>\n";
echo implode(", ", get_extension_funcs('big_int'));
echo "<br/>\n";