使用BCMath时出错 - 调用未定义的方法PEAR_Error :: int2bin()

时间:2011-12-16 06:49:25

标签: php pear bcmath

使用BCMath时出现此错误 -

  

致命错误:调用未定义的方法PEAR_Error :: int2bin()   第23行的login.php

我正在尝试一起使用Crypt_RSA和BCMath。这是我的代码 -

require_once("Crypt/RSA/MathLoader.php");
$wrapper_name = “BCMath”;
$math_obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);

$a = $math_obj->int2bin("6465130539297209249500692895930266194225707667564124686892613724438982507603215802636578141547940687986170708901198917318074984831856438115515743080726101");

1 个答案:

答案 0 :(得分:1)

所以当我几天前在php中做一些加密时,我遇到了类似的问题。我需要将十进制数转换为二进制数。我所做的是将其转换为十六进制,然后将其解压缩为十六进制编码数据。

<?php 

$a = pack("H*", convBase('6465130539297209249500692895930266194225707667564124686892613724438982507603215802636578141547940687986170708901198917318074984831856438115515743080726101', '0123456789', '0123456789ABCDEF'));

function convBase($numberInput, $fromBaseInput, $toBaseInput)
{
    if ($fromBaseInput==$toBaseInput) return $numberInput;
    $fromBase = str_split($fromBaseInput,1);
    $toBase = str_split($toBaseInput,1);
    $number = str_split($numberInput,1);
    $fromLen=strlen($fromBaseInput);
    $toLen=strlen($toBaseInput);
    $numberLen=strlen($numberInput);
    $retval='';
    if ($toBaseInput == '0123456789')
    {
        $retval=0;
        for ($i = 1;$i <= $numberLen; $i++)
            $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));
        return $retval;
    }
    if ($fromBaseInput != '0123456789')
        $base10=convBase($numberInput, $fromBaseInput, '0123456789');
    else
        $base10 = $numberInput;
    if ($base10<strlen($toBaseInput))
        return $toBase[$base10];
    while($base10 != '0')
    {
        $retval = $toBase[bcmod($base10,$toLen)].$retval;
        $base10 = bcdiv($base10,$toLen,0);
    }
    return $retval;
}
?>