简体   繁体   中英

Crypt_RSA phpseclib3 PHP 8 Issue

As the title says:

We have recently upgraded to PHP 8 however, Crypt/RSA.php is deprecated and will not be supported in PHP 8 anymore, so we are forced to find another solution.

I heard that phpseclib3 is the same and able to support it, however, I cannot get it to work.

This is my current code:

 $rsa = \phpseclib3\Crypt\RSA::loadPrivateKey($private_key); $rsa = $rsa->withHash('sha256'); $rsa = $rsa->withPadding(RSA::SIGNATURE_PKCS1); $ans = $rsa->sign("test"); echo $ans;

This is the previous code that works:

 $rsa = new Crypt_RSA(); $rsa->loadKey($private_key); $rsa->setHash('sha256'); // Specify digest, eg sha256 (default is sha1) $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1); $signature = base64_encode($rsa->sign($string_to_encrypt));

with the new code, I am getting this error: Uncaught OutOfRangeException: Signature representative out of range in xxx

The Key is the same so definitely, it is not the key's problem, I am unable to figure out what was wrong, can anyone help me with this?

Any help is appreciated!

Test Key:

 -----BEGIN RSA PRIVATE KEY----- MIICXwKBAQACgYDWZIPLr6mbty5UIwUnnP8RGjZnrAA1tlQXyqOS89h9LwLwsvlr 8u+1t1iGb+J4wFU34iYTKiHiA2U5Ik5eNnHDR1r9g+UcoM98gQA67D4Ns2+lTHSn 53Bfp/6RvkOPkQq1u6E0zz6bXAXcpZX6kBPIeF4gq+vRj62tprZ90DfuSQKBAwEA AQKBgKdRfNZbOshPFvYM+rnmud3UEj+oqeVBbbG/sRc//WX9aZMGashHqbKs64bI DPxmRlu9wC9Ww2jLc5U6Y+T9IpxyCBddx93iiGVFVYxAwYMT16gp/RV5RJOh4779 z7QxDvf3h+pCMLry581Irq+qInVuMKB1NCcdfYYYcoOSLz9tAoFA6lbLDDFA++UN +xSYUOX4M+QdDpkbKc3R3tRhXRG1hD1s6cT/FZtikoK56XktE8LV9jYbfW78ukK7 mCVenBXwqwKBQOo1ufanHxjW31ntAjgh7wqpDscVW3fOtH5Yb/zbfgp70zNU6S4t o7CMMvllZq4aJIq+s/afKlLt8r4lRSjRJNsCgUBSsdQvdkYfXgJkAy2JBs5iyXHz j9YFF4ujHcFKDZDHnMGNcSYvIslTOGyQ1mEw4wWMQCXNSthGhnwawb00SY3HAoFA UsgCnsebGjB4aVR4QFbeIqp2aq+F0ObirPWdP4ZlGleB4tbURFolTTWAnt3UWR0s ZU3aVgUT9JPeBI8vzql9mQKBQOX+pnnO5iWu+F2FFzSfM7g3ENCmjSEJ7xBXXUA/ LJVdk0rQRbl1XojjSEGp85NS4EncWo1JFyAXBh7ZC24LAzc= -----END RSA PRIVATE KEY-----

Found the solution, can be done via OpenSSL RSA Sign.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM