在 phpseclib 中使用指数和模数生成签名

Generate signature using exponent and modulus in phpseclib

正在尝试使用 PHPSECLib

生成明文签名
$rsa = new \phpseclib\Crypt\RSA();
$modulus = '3vDug4ykxU62V4fcIFMuqrd71H92LiyRhOsiNAMqRK40qj4RYdMIXYGmqZq7oBXJ3GNQEBNqZ2t4D5004wY35yPx6LVFU54No6SdoE4E1fUB5gLrLAUcil0b-k_KN9gJNndcfYyUIKwRJywUQda35pwiTI1tvE41_Lk6HGOLtSauh7ichwgghbCZFzc_Gx0XWrBUXWhYCCW2_CTPWWqfhNI7qMJvBqfmoOunMD_pIozrkGUx6dgaIjbaICBJCEheFFiwHqYRUOXHCHxsKRBzHx-5vlsedj5HnI3FMOF9yNbafwk_SfPZQrZC0T7OwQzOmj9IngRGOmg3lwuDlSCcwQ';
$exponent = 'AQAB';
$public = [
    'n' => new \phpseclib\Math\BigInteger($modulus, 16),
    'e' => new \phpseclib\Math\BigInteger($exponent, 16),
];
$rsa->loadKey($public);
$text = 'RohanSakhale';
$signature = $rsa->sign($text);
var_dump($signature);

这里得到空签名,请指出哪里出错了。

BigInteger 构造函数的第二个参数为 16 时,它告诉 BigInteger 假设字符串是十六进制编码的数字。但是你的字符串不是十六进制编码的——它们是 base64 编码的。例如,模数的第二个字符是 v。那不是有效的十六进制字符。试试这个:

$rsa = new \phpseclib\Crypt\RSA();
$modulus = '3vDug4ykxU62V4fcIFMuqrd71H92LiyRhOsiNAMqRK40qj4RYdMIXYGmqZq7oBXJ3GNQEBNqZ2t4D5004wY35yPx6LVFU54No6SdoE4E1fUB5gLrLAUcil0b-k_KN9gJNndcfYyUIKwRJywUQda35pwiTI1tvE41_Lk6HGOLtSauh7ichwgghbCZFzc_Gx0XWrBUXWhYCCW2_CTPWWqfhNI7qMJvBqfmoOunMD_pIozrkGUx6dgaIjbaICBJCEheFFiwHqYRUOXHCHxsKRBzHx-5vlsedj5HnI3FMOF9yNbafwk_SfPZQrZC0T7OwQzOmj9IngRGOmg3lwuDlSCcwQ';
$exponent = 'AQAB';
$public = [
    'n' => new \phpseclib\Math\BigInteger(base64_decode($modulus), 256),
    'e' => new \phpseclib\Math\BigInteger(base64_decode($exponent), 256),
];
$rsa->loadKey($public);
$text = 'RohanSakhale';
$signature = $rsa->sign($text);
var_dump($signature);