【问题标题】:Does Security:cipher encrypted result depend on the server?Security:cipher 加密结果是否取决于服务器?
【发布时间】:2011-06-24 20:00:59
【问题描述】:

我在一个 cakephp 应用程序中工作,我使用 Security::cipher 来加密一些数据。它运行良好,但我已将文件和数据库移动到另一台服务器,现在加密结果不同。 我尝试了一些简单的线条:

$security = new Security;
$code = $security->cipher('1234', Configure::read('Security.cipherSeed'));

当我打印 $code 时,两个服务器中的值不同。我在两个 core.php 文件中配置了相同的 Security.cipherSeed。 Security::cipher 函数是否使用某些服务器值进行加密?

谢谢。

【问题讨论】:

    标签: php security cakephp encryption platform-independent


    【解决方案1】:

    好吧,看看this bug,这似乎是个问题。

    深入了解the source code,这一行就是它的工作原理:

    srand(Configure::read('Security.cipherSeed'));
    

    现在,为什么会这样?因为rand() 实现了伪随机算法。所以对于任何给定的已知种子,理论上你可以产生同一系列的随机输出。看看这是否可行,让我们看看the PHP source code for rand(),特别是内部php_rand 函数:

    PHPAPI long php_rand(TSRMLS_D)
    {
        long ret;
    
        if (!BG(rand_is_seeded)) {
                php_srand(GENERATE_SEED() TSRMLS_CC);
        }
    

    我们知道这不是问题,因为我们是手动播种(除非我们在服务器上安装了 suhosin 补丁,否则它总是会重新播种,因此无法正常工作)。

    #ifdef ZTS
        ret = php_rand_r(&BG(rand_seed));
    #else
    # if defined(HAVE_RANDOM)
        ret = random();
    # elif defined(HAVE_LRAND48)
        ret = lrand48();
    # else
        ret = rand();
    # endif
    #endif
    

    哇,你看到发生了什么吗?根据服务器规范,可以使用 4 个不同的随机库之一(rand()random()lrand48() 或它自己的内部随机函数 php_rand_r)!这就是为什么它不能跨服务器安装移植。

    改为使用真正的加密库,例如MCryptGPG

    编辑:我已经向 cake 提交了关于这个主题的 bug report

    【讨论】:

    • 谢谢ircmaxell。我看了一下密码函数,但我不知道它是如何工作的。我会试试那些加密库。
    • 我也有这个,换服务器时无法登录或使用cookies!
    猜你喜欢
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多