jump to navigation

Simple Encryption Class May 22, 2006

Posted by rossoft in CakePHP.
trackback

A class for encryption of text (reversible encryption).

Usage:
vendor(‘crypt’ . DS . ‘simple_crypt’);
$this->SimpleCrypt=& new SimpleCrypt();
$encrypted=$this->SimpleCrypt->encrypt(‘a_key’,’the_text’);
$plain=$this->SimpleCrypt->decrypt(‘a_key’,$encrypted);

You can use a different engine if you don’t have Mcrypt:
$this->SimpleCrypt=& new SimpleCrypt(‘Simple’);

Copy to /vendors/crypt/simple_crypt.php
<?php
/**
 * SimpleCrypt Class
 * Simple encryption of strings
 *
 * Usage:
 * $this->SimpleCrypt=& new SimpleCrypt();
 * $encrypted=$this->SimpleCrypt->encrypt(‘a_key’,’the_text’);
 * $plain=$this->SimpleCrypt->decrypt(‘a_key’,$encrypted);
 *
 * The result is $plain equals to ‘the_text’
 *
 * You can use a different engine if you don’t have Mcrypt:
 * $this->SimpleCrypt=& new SimpleCrypt(‘Simple’);
 *
 * A string encrypted with one engine can’t be decrypted with
 * a different one even if the key is the same.
 *
 * @author RosSoft
 * @version 0.2
 * @license MIT
 */

class SimpleCrypt extends Object
{
    /**
     * Constructor
     * @param string $engine_name Engine for encryption. Values: Simple, Mcrypt
     */       
    function __construct($engine_name=’Mcrypt’)
    {
        $engine_name=’SimpleCrypt’ . $engine_name . ‘Engine’;
        $this->_engine=new $engine_name;
        parent::__construct();
    }
   
    /**
     * Encrypts the string with the given key
     * @param string $key
     * @param string $string Plaintext string
     * @return string Ciphered string
     */   
    function encrypt($key, $string)
    {
        return $this->_engine->encrypt($key,$string);       
    }
   

    /**
     * Decrypts the string by the given key
     * @param string $key
     * @param string $string Ciphered string
     * @return string Plaintext string 
     */
    function decrypt($key, $string)
    {
        return $this->_engine->decrypt($key,$string);
    }
}

/**
 * Simple Engine doesn’t need any PHP extension.
 * Every encryption of the same string with the same key
 * will return the same encrypted string
 */
class SimpleCryptSimpleEngine extends Object
{
    function encrypt($key, $string)
    {
        $result = ”;
        for($i=1; $i<=strlen($string); $i++)
        {
            $char = substr($string, $i-1, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)+ord($keychar));
            $result.=$char;
        }
        return $result;       
    }

    function decrypt($key, $string)
    {
        $result = ”;
        for($i=1; $i<=strlen($string); $i++)
        {
            $char = substr($string, $i-1, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)-ord($keychar));
            $result.=$char;
        }
        return $result;
    }       
}

/**
 * McryptEngine requires Mcrypt extension
 * Every encryption of the same string with the same key
 * will return a different encrypted string.
 */
class SimpleCryptMcryptEngine extends Object
{
    var $alg=MCRYPT_BLOWFISH;

    function encrypt($key, $string)
    {
        $td = mcrypt_module_open ($this->alg, ”, ‘ecb’, ”);
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td),MCRYPT_DEV_RANDOM);       
        $encrypted_data = mcrypt_ecb($this->alg, $key,$string,MCRYPT_ENCRYPT,$iv);
        return $iv . $encrypted_data;
    }

    function decrypt($key, $string)
    {
        $td = mcrypt_module_open ($this->alg, ”, ‘ecb’, ”);
        $iv = substr($string,0,mcrypt_enc_get_iv_size($td));
        $string=substr($string,mcrypt_enc_get_iv_size($td));       
        return mcrypt_ecb($this->alg, $key,$string,MCRYPT_DECRYPT,$iv);
    }
}
?>

Advertisements

Comments»

1. RosSoft » Cookie Component - May 22, 2006

[…] For easy working with cookies. It has encryption support through SimpleEncryption class […]

2. zyklone - March 5, 2007

dont make the encryption simple. the user needs more relevant encryption

3. ureshpatel - December 30, 2009

thank you so much..its good class ….
i am just beginner for the PHPCAKE.


Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: