CodeIgniter中的加密密钥

问题描述

| 如果要使用Session类,则CodeIgniter 2.0.2需要在配置文件中设置加密密钥,即ѭ0set。可以是任何字符串吗?安全的crypto_key的任何示例? 谢谢。     

解决方法

           密钥应尽可能随机,并且不能是常规密钥   文本字符串,也不输出哈希函数等。 要将密钥保存到application / config / config.php,请打开文件并进行以下设置:
$config[\'encryption_key\'] = \'yourKeyHere\'
随机密钥生成器   重要的是,您必须知道加密功能生成的编码消息大约是原始消息的2.6倍。例如,如果您对长度为21个字符的字符串“我的超级机密数据”进行加密,则最终会得到一个大约55个字符的编码字符串(我们说“大致”是因为字符串长度在64位簇中递增,因此不是完全线性的)。选择数据存储机制时,请记住此信息。例如,Cookie只能保存4K信息。     ,        除了Chumillas的回答之外,我还亲自将随机密钥生成器用于CodeIgniter加密字符串。快捷方便。     ,        Codeigniter 3.1.0 您不得将常规文本用于\'encryption_key \' \“密钥应尽可能随机,并且不能是常规文本字符串,也不能是哈希函数的输出等。要创建正确的密钥,必须使用加密库的create_key()方法\”
$this->load->library(\'encryption\');
$key = $this->encryption->create_key(16);
// Get a hex-encoded representation of the key:
$key = bin2hex($this->encryption->create_key(16));

// Put the same value in your config with hex2bin(),// so that it is still passed as binary to the library:
$config[\'encryption_key\'] = hex2bin(<your hex-encoded key>);
来源:https://codeigniter.com/userguide3/libraries/encryption.html#setting-your-encryption-key     ,        在您的终端中输入:
php -r \'echo bin2hex(random_bytes(16)),\"\\n\";\'
它会在您更新config.php的地方输出一个字符串     ,        只需转到应用程序/配置 打开config.php文件 找出单词
$config[\'encryption_key\'] = \'\';
替换为
$config[\'encryption_key\'] = \'your_encryption_key_here\';
    ,        我在应用程序的安装程序中使用以下代码。它需要128个字节的随机数据(转换为十六进制字符串),一次需要两个字符,然后转换为十进制,并检查它们是否在可接受的范围内(字母数字,带符号,无空格或不会赢得\ “不能与您的编辑器或配置文件配合使用-aka否\') 32个字符为128位,因此可以与分组密码一起很好地工作。
function random_key_string() {
    $source = bin2hex(openssl_random_pseudo_bytes(128));
    $string = \'\';
    $c = 0;
    while(strlen($string) < 32) { 
        $dec = gmp_strval(gmp_init(substr($source,$c*2,2),16),10);
        if($dec > 33 && $dec < 127 && $dec !== 39)
            $string.=chr($dec);
        $c++;
    }
    return $string;
}
    ,        要将密钥保存到application / config / config.php,请打开文件并进行以下设置: 在线227
$config[\'encryption_key\'] = \"YOUR KEY\";