如何使用phpseclib和RSA仅使用模数和指数加密文本?

时间:2014-03-12 00:38:05

标签: php encryption rsa phpseclib

我尝试了答案to this question,但它不起作用:

$rsa = new \Crypt_RSA();
$rsa->loadKey(
    array(
        'e' => new \Math_BigInteger($exponent),
        'n' => new \Math_BigInteger($modulus)
    )
);
$cipher = $rsa->encrypt($text, CRYPT_RSA_PRIVATE_FORMAT_PKCS1);

我的模数是一个512个字符的十六进制数,我的指数是10001.我已经尝试了很多不同的解决方案,但我还没有找到一个使这个工作的方法。我收到此错误:

Notice: Uninitialized string offset: 0 in BigInteger.php on line 547

1 个答案:

答案 0 :(得分:3)

当您使用表示十六进制数字的字符串时,您应该告诉Math_BigInteger

$rsa = new Crypt_RSA();
$rsa->loadKey(
    array(
        'e' => new Math_BigInteger($exponent, 16),
        'n' => new Math_BigInteger($modulus, 16)
    )
);
相关问题