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

时间:2017-12-13 08:52:13

标签: php security rsa 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);

在此处获取空签名,请在此处说明哪个步骤出错。

1 个答案:

答案 0 :(得分:2)

BigInteger构造函数的第二个参数为16时,告诉BigInteger假设字符串是十六进制编码的数字。但是你的字符串不是十六进制编码的 - 它们是基于64位编码的。例如,模数的第二个字符是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);