0

I'm using Composer and this is my first time trying to use phpseclib. I can't find the docs or any wiki about it, it's like there is no information.

I tried searching on stackoverflow but still couldn't get it to work.

This is my .php file:

<?php
require __DIR__ . '/../vendor/autoload.php';

$loader = new Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . '/../vendor/phpseclib/');
$loader->register();

use phpseclib\Crypt\RSA;

$rsa = new Crypt_RSA();

$plaintext = 'terrafrost';

$rsa->loadKey($privatekey);
$signature = $rsa->sign($plaintext);

$rsa->loadKey($publickey);
echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
?>

This is the error: [23-Mar-2020 14:04:51 UTC] PHP Fatal error: require(): Failed opening required '[23-Mar-2020 00:53:12 UTC] PHP Fatal error: Uncaught Error: Class 'Crypt_RSA' not found in /home/123/domains/asd.com/public_html/Auth/test.php:93

  • This .php is located in: public_html/Auth
  • Autoload is located in: public_html/vendor/autoload.php
  • Composer folder is located in: public_html/vendor/composer

All I need to do is RSA signing and verifying. Also need to be able to generate a key pair for RSA too.

Please if there is any documentation or wiki about phpseclib tell me. I couldn't find anything on their Github. They link to this page Sourceforge but it's for 1.0, I only could find 1 example for 2.0. It's like the worst documented library I've ever seen.

If anyone knows a better library please tell me, I'm using this one instead of openssl because I need PSS padding.

2
  • You didn't actually say what the problem you're having is. I can guess, based on the code you posted, but, none-the-less, when posting questions like this in the future, it is something you should do.
    – neubert
    Commented Mar 23, 2020 at 13:23
  • My bad, I forgot to add the error, wasn't intentional. I will edit the post
    – Carlos86
    Commented Mar 23, 2020 at 14:10

1 Answer 1

0

Assuming you setup Composer correctly, then try this:

<?php
require __DIR__ . '/vendor/autoload.php';

use phpseclib\Crypt\RSA;

$rsa = new RSA();

$plaintext = 'terrafrost';

$rsa->loadKey($privatekey);
$signature = $rsa->sign($plaintext);

$rsa->loadKey($publickey);
echo $rsa->verify($plaintext, $signature) ? 'verified' : 'unverified';
?>

The key difference from the code in your post is that instead of doing $rsa = new Crypt_RSA(); it's doing $rsa = new RSA();. It also removes the unnecessary auto-loading stuff. No doubt you got that extra code from http://phpseclib.sourceforge.net/2.0.html but that code doesn't include __DIR__ . '/vendor/autoload.php' - it includes ClassLoader.php renamed as autoload.php.

Please if there is any documentation or wiki about phpseclib tell me. I couldn't find anything on their Github. They link to this page Sourceforge but it's for 1.0, I only could find 1 example for 2.0.

When you visit any of the pages with examples you should get a popup that looks like this:

enter image description here

Select 2.0 and then you'll see examples for 2.0.

It's like the worst documented library I've ever seen.

I guess you haven't seen https://www.openssl.org/docs/ lol. Anyway, two comments on this:

2
  • 1
    Hello, thank you, it fixed it. (had to change '/vendor' to '/../vendor'). Now I have another problem, it says unverified. I checked both keys and they are correct. Also where can I read about this sign / verify function? I need to change the algorithm, padding, etc.
    – Carlos86
    Commented Mar 23, 2020 at 14:09
  • 1
    Nvm, it took me 1 hour to figure out that I had a typo in my variable name. Sorry.
    – Carlos86
    Commented Mar 23, 2020 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.