Codeigniter-如何在config.php中设置加密密钥

时间:2018-09-04 01:51:31

标签: php codeigniter encryption codeigniter-3

我已经阅读了Codeigniter的文档,了解如何使用加密库。但是,我在初始化它时有疑问,我不确定应该在哪里调用该库并创建密钥。我曾尝试如下所示在config.php中初始化密钥,但是却收到错误消息:在core / Codeigniter.php中找不到CI_Controller。

$CI =& get_instance();
// Get a hex-encoded representation of the key:
$key = bin2hex($CI->encryption->create_key(16));
$config['encryption_key'] = hex2bin($key);

2 个答案:

答案 0 :(得分:1)

要添加加密密钥,您必须执行以下步骤:

初始化类:

public static void main(String[] args) {
    if (args.length == 0) {
        // User didn't provide number of files.
        // You need to prompt the user and read their input.
    } else if (args.length == 1) {
        int numFiles = Integer.parseInt(args[0]);
        System.out.println("You selected " + numFiles + " files.");
    } else {
        // User provided two or more command-line arguments.
        // You might want to print an error message in this case.
    }
}

密钥应该尽可能随机,并且不能是常规文本字符串,也不能是哈希函数的输出等。为了创建适当的密钥,必须使用加密库的create_key()方法< / p>

$this->load->library('encrypt');

要将密钥保存到application / config / config.php中,请打开文件并设置:

$key = $this->encryption->create_key(16);

我希望它对您有用。

答案 1 :(得分:0)

为什么不通过控制器进行更改?你可以做到

function changeEncryption_key(){
    $this->load->library('encryption');
    $key = $this->encryption->create_key(16);
    $key = bin2hex($this->encryption->create_key(16));
    $config['encryption_key'] = hex2bin($key);
}