Using phpseclib with Yii

I installed phpseclib because I needed to do some AES encryption in my project. After all the hassle of installing php lib, I found out that Yii already has a security module (securityManager). I finally decided to keep using phpseclib because it has one big advantage over Yii securityManager module, it does not requite mcrypt to be installed in the phpserver!

Installing phpseclib is not as straightforward as you could imagine. Yii lazy loading (I think it is called like that) is not compatible with phpseclib files because classes are not called the same as the file names they are in.

Edit: As yiqing95 pointed out, the reason the names are not the same is because of a zend and pear naming convention so the zend autoloader should be used instead of my approach of modifying the third party library. Proceed at your own risk, but you better read yiqing95 comment.

-Steps to install phpseclib (modifying the library, not recommended now)-

(written on 2/Oct/2012, using Yii version 1.1.12. and phpseclib 0.3.0)

1) download phpseclib from: http://phpseclib.sourceforge.net

2) uncompress the downloaded archive, rename the folder to "phpseclib", and put it into your /vendors project directory.

3) rename all the files under /vendors/phpseclib/Crypt like this:

AES.php -> Crypt_AES.php

DES.php -> Crypt_DES.php

etc... (just prepend "Crypt_" in the file name)

4) search for "require_once" commands in those files and change them so they point to the new file names.

For example, in Crypt_AES.php change:

if (!class_exists('Crypt_Rijndael')) {
    require_once 'Rijndael.php';
}

to:

if (!class_exists('Crypt_Rijndael')) {
    require_once 'Crypt_Rijndael.php';
}

5) in config/main include phplib:

'import'=>array(
		...
                'application.vendors.phpseclib.Crypt.*',
               ),

6) now you can use phplib in your project. For example within a model you can use:

public function encrypt($text){
  $cipher = new Crypt_AES();
  $cipher->setPassword('anypassword');
  return bin2hex($cipher->encrypt($text));
}

(see phplib documentation for more details)

0 0
6 followers
Viewed: 11 956 times
Version: 1.1
Category: How-tos
Tags: security
Written by: nkd
Last updated by: nkd
Created on: Oct 2, 2012
Last updated: 5 years ago
Update Article

Revisions

View all history

Related Articles