Welcome Guest, Not a member yet? Register   Sign In
URI-Safe Encrypt Library Extension
#1

[eluser]TheFuzzy0ne[/eluser]
I extended the CI_Encrypt class, as I wanted to have the ability to send an encrypted string over the URI, without triggering the infamous "The URI you submitted has disallowed characters." message. I use it for my "Forgot Password" module on my Web site. I encrypt the users Email address, and the last 16 characters of their MD5 hashed password, and this is then decoded and used to validate the user.

I hope someone might find it useful.

Code:
<?php
/**
* This class extends the core Encrypt class, and allows you
* to use encrypted strings in your URLs.
*/
class MY_Encrypt extends CI_Encrypt
{
    /**
     * Encodes a string.
     *
     * @param string $string The string to encrypt.
     * @param string $key[optional] The key to encrypt with.
     * @param bool $url_safe[optional] Specifies whether or not the
     *                returned string should be url-safe.
     * @return string
     */
    function encode($string, $key="", $url_safe=TRUE)
    {
        $ret = parent::encode($string, $key);
        
        if ($url_safe)
        {
            $ret = strtr(
                    $ret,
                    array(
                        '+' => '.',
                        '=' => '-',
                        '/' => '~'
                    )
                );
        }
        
        return $ret;
    }
    
    /**
     * Decodes the given string.
     *
     * @access public
     * @param string $string The encrypted string to decrypt.
     * @param string $key[optional] The key to use for decryption.
     * @return string
     */
    function decode($string, $key="")
    {
        $string = strtr(
                $string,
                array(
                    '.' => '+',
                    '-' => '=',
                    '~' => '/'
                )
            );
            
        return parent::decode($string, $key);
    }
}

// End of file: MY_Encrypt.php
// Location: ./system/application/helpers/MY_Encrypt.php
#2

[eluser]codex[/eluser]
I find it useful. Thanks!
#3

[eluser]Edmundas KondraĊĦovas[/eluser]
Thank you for sharing this with us. I already know where I could implement it in one of my projects. Wink
#4

[eluser]pmoroom[/eluser]
Wonderful!!! this is exactly what I was looking for. It works perfectly.
#5

[eluser]jeffpeck[/eluser]
Excellent, a nice solution to a common issue. I am just about to use it myself.
#6

[eluser]fserrano[/eluser]
It might be better to encode and decode to base64 before replacing the characters. I was having some issues with this library until I combined it with TheFuzzyOne's similar code in the helper found here: http://ellislab.com/forums/viewthread/107741/

Here's what I'm using:
Code:
<?php
class MY_Encrypt extends CI_Encrypt
{
    
    function encode($str="")
    {
        return strtr(
                base64_encode($str),
                array(
                    '+' => '.',
                    '=' => '-',
                    '/' => '~'
                )
            );
    }
    function decode($str="")
    {
        return base64_decode(strtr(
                $str,
                array(
                    '.' => '+',
                    '-' => '=',
                    '~' => '/'
                )
            ));
    }
}
?>

Let me know what you think.
#7

[eluser]Kelvin86[/eluser]
Hi,


I just implemented the My_Encrypt helper class in my website. There is just one problem. Codeigniter gives me an error about:

Code:
Class 'CI_Encrypt' not found.....

The class My_Encrypt is being autoloaded (config.php)

Am I the only one with this problem? How Can I fix this?
#8

[eluser]jeffpeck[/eluser]
You need to specify your own subclass prefix. As stated in the manual (bottom of here: http://ellislab.com/codeigniter/user-gui...aries.html):

To set your own sub-class prefix, open your application/config/config.php file and look for this item:
Code:
$config['subclass_prefix'] = 'MY_';
#9

[eluser]jeffpeck[/eluser]
Actually, Kelvin86, it sounds like you actually replaced the CI Encrypt class. Make sure that you put it into the /System/Application/Libraries/ folder and not the /System/Libraries folder.
#10

[eluser]Kelvin86[/eluser]
Thanks.
I putted in the Application/helper/ folder first because on the end of MY_Encrypt file it said so.
But oke now the problem is fixed.

But there is another problem with the decoded part.
I got in my controller:

Code:
function readInbox($from_id, $email_id)
    {
        $data['base_url']    =    base_url();
        $data['title']        =    'SmsAruba - Read Inbox';
        
        $from_id_decrypted    =    $this->encrypt->decode($from_id); //returns BLANK
        
        echo $this->encrypt->decode('J4zHs8hZJexjcOq4bcIYoLY9Ko9Xw2ygb7rSTBuU7E8JKmt1dFRwMnKWVHLtjAq5R6CYFL~o.gS2OEeQiKQQSg--'); //<<< - the value of the encrypted $from_id
        //this returns "2" ... this is what I want!
    }

Why is this happening????? :S

*Edit: The same goes with $email_id




Theme © iAndrew 2016 - Forum software by © MyBB