Difference between #4 and #5 of Using phpseclib with Yii

unchanged
Title
Using phpseclib with Yii
unchanged
Category
How-tos
unchanged
Tags
security
changed
Content
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](http://www.yiiframework.com/doc/api/1.1/CApplication#securityManager
"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**-Steps to install phpseclib (modifying the
library, not recommended now)-**phpseclib-**

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

1) download phpseclib from:
[http://phpseclib.sourceforge.net](http://phpseclib.sourceforge.net
"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:
~~~
[php]
if (!class_exists('Crypt_Rijndael')) {
    require_once 'Rijndael.php';
}
~~~
to:
~~~
[php]
if (!class_exists('Crypt_Rijndael')) {
    require_once 'Crypt_Rijndael.php';
}
~~~

5) in **config/main** include phplib:
~~~
[php]
'import'=>array(
		...
                'application.vendors.phpseclib.Crypt.*',
               ),
~~~

6) now you can use phplib in your project. For example within a model you can
use:
~~~
[php]
public function encrypt($text){
  $cipher = new Crypt_AES();
  $cipher->setPassword('anypassword');
  return bin2hex($cipher->encrypt($text));
}
~~~
(see phplib documentation for more details)