gpt4 book ai didi

php - 为什么 kohana 3 Encrypt 类 encode() 方法每次都返回不同的字符串?

转载 作者:搜寻专家 更新时间:2023-10-31 20:50:11 25 4
gpt4 key购买 nike

我在 Encrypt 类中执行 encode(),每次它为相同的输入字符串返回不同的字符串。我的 application/config/encrypt.php:

return array(
'default' => array(
/**
* The following options must be set:
*
* string key secret passphrase
* integer mode encryption mode, one of MCRYPT_MODE_*
* integer cipher encryption cipher, one of the Mcrpyt cipher constants
*/
'cipher' => MCRYPT_RIJNDAEL_128,
'key' => 'df58e28f',
'mode' => MCRYPT_MODE_NOFB,
),
);

使用:

$str = Encrypt::instance()->encode('test');

$str 始终具有不同的值。这是一个错误还是应该以这种方式工作?为什么?

我还必须补充一点,每次我都可以decode()那个值并得到test字符串。

更新:这是一个示例输出:0vahDa/2Qu3XQWObkjwLPoL73g==

非常感谢。

最佳答案

每次不同的原因是当调用 encode() 时使用新的随机 IV 来加密数据。这是执行此操作的行:

// Create a random initialization vector of the proper size for the current cipher
$iv = mcrypt_create_iv($this->_iv_size, Encrypt::$_rand);

然后它最终返回一个由加密数据 IV 组成的 base 64 编码字符串。

// Use base64 encoding to convert to a string
return base64_encode($iv.$data);

这是故意的行为,并不是坏事。正如您所指出的,您总是会得到相同的未加密数据 - 所以它正在做它的工作。

关于php - 为什么 kohana 3 Encrypt 类 encode() 方法每次都返回不同的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8527105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com