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

发布于 2024-12-21 11:36:16 字数 901 浏览 5 评论 0原文

我正在从 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==

非常感谢。

I'm doing a encode() from the Encrypt class and each time it returns a different string for the same input string.
My 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,
    ),
);

Use:

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

$str has always a different value. Is that an error or it's supposed to work that way? Why?

Also I must add that I can always decode() that value and get the test string each time.

UPDATE: Here is a sample output: 0vahDa/2Qu3XQWObkjwLPoL73g==

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眼睛不忧郁 2024-12-28 11:36:16

每次都不同的原因是,当调用 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);

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

The reason it's different each time is that when encode() is called a new random IV is used to encrypt the data. Here's the line that does it:

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

It then ultimately returns a base 64 encoded string consisting of the encrypted data and IV.

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

It's intentional behaviour, and not a bad thing. As you noted, you always get the same unencrypted data back - so it's doing its job.

你げ笑在眉眼 2024-12-28 11:36:16

其实这是故意的行为。

恕我直言,这样加密更安全。大多数伪黑客会认为不同的加密值意味着不同的实际值。

重要的是解密后你总是得到相同的值。

您必须小心:当您想两次使用相同的加密字符串时,您应该单独存储它,因为再次加密不会产生相同的结果。

Actually this is intentional behaviour.

IMHO this way the encryption is more secure. Most pseudo hackers would think different encrypted values mean different actual values.

The important thing is that you always get the same value upon decryption.

You have to be careful with that: when you want to use the same encrypted string twice you should store it separately, because encrypting again would not produce the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文