【问题标题】:Codeigniter encryption without slash不带斜线的 Codeigniter 加密
【发布时间】:2017-01-14 14:23:55
【问题描述】:

我知道这个问题可能看起来像重复:Ignore slash while using encryption in Codeigniter。但我仍然没有从中得到答案。

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

问题是:

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

最后,我找到了一些Ignore Slash while using encryption in codeigniter - GitHub

但它给了我这个错误:Undefined property: CI_Loader::$my_encrypt

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

  1. 类名首字母大写。
  2. 使用与类名相同的文件名。 (也大写)
  3. 将扩展更改为 CI_Encryption,因为 Encrypt 类已被弃用。
  4. 在所有方法之前插入public function __construct() {parent::__construct();}
  5. 将文件放在应用程序/库中。
  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 库进行扩展

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您是否加载了库?在应用程序库中将库命名为 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;
    

    【讨论】:

    • 糟糕,我忘了告诉我在加载方法之前我已经加载了库。问题已编辑,仍然有相同的错误。顺便说一句,谢谢您的回答。
    【解决方案2】:

    已修复以扩展 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);
    }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多