没有斜杠的编码器加密


Codeigniter encryption without slash

我知道这可能看起来像这个问题的重复:在Codeigniter中使用加密时忽略斜杠。但我仍然没有得到答案。

我想发送加密的电子邮件名称作为URL到他们的电子邮件帐户。然后,该URL被解密,以搜索该电子邮件名称是否存在于我的数据库中,以允许该电子邮件进入我的系统。

问题是:

  1. 如果我在加密后使用urlencodebase64_encode,解密后总是导致空值搜索数据库。我认为这是因为加密的值总是在变化。
  2. 如果我使用临时加密,它可能有("/")字符。
  3. 如果我只使用编码而不使用加密,它可能允许电子邮件名称访问我的系统。

最后,我发现了一些:在codeigniter - GitHub中使用加密时忽略斜杠。

但是它给了我这个错误:未定义属性:CI_Loader::$my_encrypt

我不知道我做错了什么,我已经:

  1. 类名首字母大写
  2. 使用与类名相同的文件名。(大写)
  3. 将扩展名更改为CI_Encryption,因为Encrypt类已经弃用了。
  4. 在所有方法之前插入public function __construct() {parent::__construct();}
  5. 将文件放到application/library中。
  6. 加载库$this->load->library('my_encrypt');
  7. 使用$this->my_encrypt->encode($key);加载方法,这是给我一个错误的行。

我知道这可能听起来像一个简单的错误,但我也在使用另一个第三方库,但它根本没有给我一个错误。

有谁能帮我找出错误/遗漏的步骤吗?


-在控制器中加载库之前,我想先在视图中检查结果。但它不会给我任何改变,即使我把代码放入控制器。下面是代码:

    $key = 'example@gmail.com';
    $this->load->library('my_encrypt');
    $segment = $this->my_encrypt->encode($key);  
    echo $segment; 
    echo ( $this->my_encrypt->decode($segment) );

:修复库代码扩展为CI_Encryption库

加载库了吗?在应用程序库中将库命名为MY_Encrypt.php

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

现在调用加密库并使用加密类而不是my_encrypt

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

修复了扩展CI_Encryption库,抱歉打扰了。:)

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