deemer
deemer

Reputation: 35

Codeigniter encryption without slash

I know this may look like duplicate from this question: Ignore slash while using encryption in Codeigniter. But I still didn't have the answer from it.

I want to sent encrypted email name as URL to their email account. Then that URL is decrypted to search if that email name is exist in my database to permit that email into my system.

The problem is:

  1. If I use urlencode or base64_encode after encryption, it always resulted in empty value to search the database after decrypt. I think it because the encrypted value always changing.
  2. If I use the casual encryption, it might have the ("/") character.
  3. If I only use the encode, without the encryption, it might permit the email name to have access into my system.

Lastly, I found some library: Ignore Slash while using encryption in codeigniter - GitHub .

But it gave me this error: Undefined property: CI_Loader::$my_encrypt

I don't know what I've done wrong, I already:

  1. Capitalized the class name first letter.
  2. Using the same file name with the class name. (capitalized too)
  3. Change the extend to CI_Encryption because the Encrypt class is already deprecated.
  4. Insert the public function __construct() {parent::__construct();} before all method.
  5. Place the file inside application/library.
  6. Load the library $this->load->library('my_encrypt');
  7. Load the method using $this->my_encrypt->encode($key); this is the line that gave me an error.

I know that this may sound like a simple mistake, but I'm using another third-party library too but it didn't give me an error at all.

Can anyone help me find the mistake / missing step there?


Update - Before I load the library in the controller, I want to check the result first in view. But it doesn't give me any changes even when I put the code inside controller. Here is the code :

    $key = '[email protected]';
    $this->load->library('my_encrypt');

    $segment = $this->my_encrypt->encode($key);  
    echo $segment; 
    echo ( $this->my_encrypt->decode($segment) );

Update: Fix library code to extend with CI_Encryption library

Upvotes: 1

Views: 3077

Answers (2)

deemer
deemer

Reputation: 35

fixed to extend the CI_Encryption library, sorry for bothering. :)

class MY_Encrypt extends CI_Encryption
{
   /**
 * 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
 */
public function __construct() {
parent::__construct();
}

function encode($string)
{
    $ret = parent::encrypt($string);

    if ( !empty($string) )
    {
        $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)
{
    $string = strtr(
            $string,
            array(
                '.' => '+',
                '-' => '=',
                '~' => '/'
            )
    );

    return parent::decrypt($string);
}
}
?>

Upvotes: 2

urstrulyani
urstrulyani

Reputation: 66

Have you loaded the library? Name librabry as MY_Encrypt.php in application libraries

<?php

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
     */
    public function __construct() {
        parent::__construct();
    }

    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);
    }
}
?>

Now call the encrypt library and use the encryption class instead of my_encrypt

$key='Welcome';
    $this->load->library('encrypt');
    $key1= $this->encrypt->encode($key);

    echo $key1;

Upvotes: 4

Related Questions