Using phpseclib with Yii

You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#5) »

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.

-Steps to install 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

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: 18 999 times
Version: Unknown (update)
Category: How-tos
Tags: security
Written by: nkd
Last updated by: nkd
Created on: Oct 2, 2012
Last updated: 11 years ago
Update Article

Revisions

View all history

Related Articles