How to include PHPSecLib

I got an interesting question on my blog post, RSA cryptography in PHP (How To?). Even the example, I have provided works fine with the source I have provided, you would probably get into some issues when trying it from the scratch. Including PHPSecLib is not straight forward. You cannot just use include() or include_once() functions directly. It follows PEAR standard (PHP Extension and Application Repository), therefore you need to use the following trick if you dont have PEAR installed in your server or in your LAMP/ WAMP deployment.

For an example consider following directory structure:

------- App
+ ----------------- Core
+ ----------------- Vendors

Assuming that your code lies in the “Core” directory, and you are planning to place PHPSecLib in the “Vendors” directory, you can include the following to your code so that it will find the PHPSecLib without no issue:

 
$path = '../vendors/phpseclib/'; 
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include_once('Crypt/RSA.php');

Working example:

<?PHP

$path = '../vendors/phpseclib/';
	set_include_path(get_include_path() . PATH_SEPARATOR . $path);
	include_once('Crypt/RSA.php');

$rsa = new Crypt_RSA();
$keys = $rsa->createKey(2048);

var_dump($keys);

$rsa->loadKey($keys['publickey']); // public key
$plaintext = 'I want to get encrypted !';

$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($plaintext);

var_dump($ciphertext);

$rsa->loadKey($keys['privatekey']); // private key
$text = $rsa->decrypt($ciphertext);
var_dump($text);
?>

Note that we have explicitly defined the path for the library from the current directory where the executing script is located.

If you insist on using autoload instead, it can be achieved in the following manner. You cannot straight away let the autoload find the class as the file name and the class name are different. Therefore we need to change the path as the code can locate the Crypt_RSA class, which is ../vendors/phpseclib/Crypt/RSA.php

<?PHP

spl_autoload_register(function ($class_name) {
	$path = '../vendors/phpseclib/';
	set_include_path(get_include_path() . PATH_SEPARATOR . $path);
	if($class_name='Crypt_RSA')
	{
		$class_name='Crypt/RSA';
	}
	include_once $class_name.'.php';
});

$rsa = new Crypt_RSA();
$keys = $rsa->createKey(2048);

var_dump($keys);

$rsa->loadKey($keys['publickey']); // public key
$plaintext = 'I want to get encrypted !';

$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($plaintext);

var_dump($ciphertext);

$rsa->loadKey($keys['privatekey']); // private key
$text = $rsa->decrypt($ciphertext);
var_dump($text);
?>

Hope you will find this information useful. Cheers !!

Advertisements

Please add your valuable idea below, will make a discussion, thanks !

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s