2

我想像在 suhosin 中一样加密会话数据,是否有任何图书馆提供该功能?

4

2 回答 2

4

您可以轻松地使用 mcrypt 或自定义 AES 加密来加密会话数据。最好的办法是创建一个会话包装类,在设置变量时对其进行加密。

对于密钥管理,您可以创建一个唯一密钥并将其存储在 cookie 中,这样只有用户才能解密他们自己的会话数据。

于 2009-11-08T14:26:01.003 回答
1

Zend 框架的示例实现在这里: http: //www.eschrade.com/page/encrypted-session-handler-4ce2fce4/

重要功能参考:

// $this->secredKey is stored in a cookie
// $this->_iv is created at the start
public function setEncrypted($key, $value)
{
    $_SESSION[$key] = bin2hex(
        mcrypt_encrypt(
            MCRYPT_3DES,
            $this->secretKey,
            $value,
            MCRYPT_MODE_CBC,
            $this->_iv
        )
    );
}

public function getEncrypted($key)
{
    if (isset($_SESSION[$key])) {
        $decrypt = mcrypt_decrypt(
            MCRYPT_3DES,
            $this->secretKey,
            pack(
                'H*',
                $_SESSION[$key]
            ),
            MCRYPT_MODE_CBC,
            $this->_iv
        );
        return rtrim($decrypt, "\0"); // remove null characters off of the end
    }
    return null;
}
于 2013-06-13T07:26:55.610 回答