Dave
Dave

Reputation: 5190

Unable to create instance of Crypt_RSA using phpseclib

I am trying to encrypt a string of data using the modulus and exponent provided to me by the vendor that I need to interface with. My research led me to the phpseclib library to make this easy to do.

I installed the latest version of phpseclib (3.0) using composer (php composer.phar require phpseclib/phpseclib:~3.0) and put it below a directory that is already in the PHP include path (/var/Classes).

All of the documentation I have read along with examples of how to do it resulted in the code shown below. However, the simple test fails on the 5th line when trying to create an instance of Crypt_RSA.

<?php
require 'vendor/autoload.php';
$modulus  = 'somevalue';
$exponent = 'ZZZZ';
$rsa      = new Crypt_RSA();
$modulus  = new Math_BigInteger(base64_decode($modulus), 256);
$exponent = new Math_BigInteger(base64_decode($exponent), 256);
$rsa->loadKey(['n' => $modulus, 'e' => $exponent]);
echo $rsa;

The complete error message is:

Fatal error: Uncaught Error: Class "Crypt_RSA" not found in test.php:5

There are a number of resources, both here on SO and in other places, that describe various ways to accomplish this but none of them appear to be phpseclib version 3.0 compatible/related.

Upvotes: 1

Views: 1136

Answers (1)

Dave
Dave

Reputation: 5190

Well it turns that things are quite a bit different for the version 3 API. The code below works as expected.

<?php
require 'vendor/autoload.php';
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Math\BigInteger;
$modulus  = 'somevalue';
$exponent = 'ZZZZ';
$modulus  = new BigInteger(base64_decode($modulus), 256);
$exponent = new BigInteger(base64_decode($exponent), 256);
$rsa = PublicKeyLoader::load([
    'n' => $modulus,
    'e' => $exponent
]);
echo $rsa;

Upvotes: 4

Related Questions