Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Getting Started With Mcrypt (0 Comments)
Admin: Posted Date: April 4, 2010

Mcrypt is a wonderful set of library functions for encrypting, decrypting, and hashing data. This is a great way to secure your data and encrypt it so prying eyes can't get your data.

Getting Started With Mcrypt

Mcrypt is a wonderful set of library functions for encrypting, decrypting, and hashing data. For Debian users, getting Mcrypt is apparently as easy as:

apt-get install php4-mcrypt

apacectl restart
[Note: I haven't tried this; I'm one of "the rest of us"]
For the rest of us, the process is a little more involved. I recommend building mcrypt as a dynamically loadable extension into PHP. It makes PHP more maintainable and upgradeable, since you don't have to keep recompiling.

First, install libmcrypt. As root:

# wget -c 'http://easynews.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.7.tar.gz' (OR WHATEVER THE LATEST VERSION ON YOUR NEAREST MIRROR)
# gunzip libmcrypt-2.5.7.tar.gz
# tar -xvf libmcrypt-2.5.7.tar
# cd libmcrypt-2.5.7
# ./configure --disable-posix-threads
then edit the Makefile:

CFLAGS = -g -O2 --disable-posix-threads
then continue:

# make clean
# make
# make install
Next, compile the mcrypt dynamic module. From PHP source tree of the current version of PHP running on your server:

# cd ext/mcrypt
# phpize
# aclocal
# ./configure
# make clean
# make
# make install
You should now have mcrypt.so in /usr/lib/php4

Add the line:


extension=mcrypt.so
to /etc/php.ini
and issue

# apachectl restart
Next - test it out.

I found this nested loop very useful for checking the sanity of my libmcrypt install. It turned out many of the modules weren't working in certain modes. This will tell you just what your mcrypt is capable of:

/* run a self-test through every listed cipher and mode 
*/
function mcrypt_check_sanity() {
$modes = mcrypt_list_modes();
$algorithms = mcrypt_list_algorithms();
foreach ($algorithms as $cipher) {
       if(mcrypt_module_self_test($cipher)) {
               print $cipher." ok.
\n";
       } else {
               print $cipher." not ok.
\n";
       }
       foreach ($modes as $mode) {
               if(mcrypt_test_module_mode($cipher,$mode)) {
                       $result = "ok";
               } else {
                       $result = "not ok";
               }
               print $cipher." in mode ".$mode." ".$result."
\n";
               mcrypt_module_close($td);

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: