Welcome Guest, Not a member yet? Register   Sign In
Fail Encryption Helper
#1

[eluser]vijinho[/eluser]
This is a simple helper I wrote that uses the CI encryption library to encrypt/decrypt strings and arrays. Save it as encryption in application/helpers.

Example to encrypt and decrypt a string with a given key:
Code:
$this->load->helper('encryption');
$encrypted = fail_encrypt('some text', 'mysecretkey');
$text = fail_decrypt($encrypted, 'mysecretkey');
echo("$text");

The following _array versions take an indexed array as the first parameter and only encrypt/decrypt the array keys of $data that match the values in $keys.

e.g.
Code:
// some data with some values to encrypt
$data = array(
    'name' => 'Sheikh Ahmed',
    'age' => 64,
    'password' => 'fail',
    'secret' => 'www.failme.net'    
);

// encrypt the keys in $data that match these values
$keys = array('password', 'secret');

// use this key of encryption
$ekey = 'fail';

fail_encrypt_array($data, $keys, $ekey);
// show encrypted array
var_dump($data);

// decrypt the encrypted array
fail_decrypt_array($data, $keys, $ekey)
var_dump($data);


The helper code:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Fail Encryption Helpers
*
* @package        Fail
* @subpackage    Helpers
* @category    Helpers
* @author        Vijay Mahrra <[email protected]>
* @link        http://www.designbyfail.com/
*/

// ------------------------------------------------------------------------

/**
* Encrypt Text
*
* Encrypts a string and returns an encoded version
*
* @access    public
* @param    text string  the text to encrypt
* @param   ekey string  the encryption key to use
* @param   cipher cipher to be used
* @return    string
*/
function &fail;_encrypt($text, $ekey = null, $cipher = MCRYPT_BLOWFISH)
{
    $CI =& get_instance();
    $CI->load->library('encrypt');
    $CI->encrypt->set_cipher($cipher);
    $text = trim($text);
    if (empty($text))  {
        $return = '';
        return $return;
    }
    $encrypted = (empty($ekey)) ? $CI->encrypt->encode($text) : $CI->encrypt->encode($text, $ekey);
    return $encrypted;
}


// ------------------------------------------------------------------------

/**
* Decrypt Text
*
* Decrypts an encoded string and returns the original text
*
* @access  public
* @param   text string  the text to decrypt
* @param   ekey string  the encryption key to use
* @param   cipher cipher to be used
* @return  string
*/
function &fail;_decrypt($encrypted, $ekey = null, $cipher = MCRYPT_BLOWFISH)
{
    $CI =& get_instance();
    $CI->load->library('encrypt');
    $CI->encrypt->set_cipher($cipher);
    if (empty($encrypted)) {
        $return = '';
        return $return;
    }
    $decrypted = (empty($ekey)) ? $CI->encrypt->decode($encrypted) : $CI->encrypt->decode($encrypted, $ekey);
    return $decrypted;
}

// ------------------------------------------------------------------------


/**
* Decrypt Array
*
* Decrypts an array
*
* @access  public
* @param   data array  the array with encrypted values
* @param   keys array  the values here contain the array keys in $data to decrypt
* @param   text string  the text to decrypt
* @param   ekey string  the encryption key to use
* @param   cipher cipher to be used
* @return  string
*/
function fail_decrypt_array(&$data, $keys, $ekey = null, $cipher = MCRYPT_BLOWFISH)
{
    if (!is_array($data)) {
        return false;
    }
    // decrypt all fields if no fields specified
    if (count($keys) == 0) {
        foreach ($data as $k => $v) {
            $data[$k] = fail_decrypt($data[$k], $ekey, $cipher);
        }
    } else {
        // decrypt only the fields specified
        foreach ($keys as $k) {
            if (!array_key_exists($k, $data)) continue;
            $data[$k] = fail_decrypt($data[$k], $ekey, $cipher);
        }
    }
    return true;
}

// ------------------------------------------------------------------------


/**
* Encrypt Array
*
* Encrypts an array
*
* @access  public
* @param   data array  the array with encrypted values
* @param   keys array  the values here contain the array keys in $data to encrypt
* @param   text string  the text to decrypt
* @param   ekey string  the encryption key to use
* @param   cipher cipher to be used
* @return  string
*/
function fail_encrypt_array(&$data, $keys = array(), $ekey = null, $cipher = MCRYPT_BLOWFISH)
{
    if (!is_array($data)) {
        return false;
    }
    // encrypt all fields if no fields specified
    if (count($keys) == 0) {
        foreach ($data as $k => $v) {
            $data[$k] = fail_encrypt($data[$k], $ekey, $cipher);
        }
    } else {
        // encrypt only fields specified
        foreach ($keys as $k) {
            if (!array_key_exists($k, $data)) continue;
            $data[$k] = fail_encrypt($data[$k], $ekey, $cipher);
        }
    }
    return true;
}
// ------------------------------------------------------------------------
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB