【问题标题】:How to save encrypted data in cookie (using php)?如何将加密数据保存在 cookie 中(使用 php)?
【发布时间】:2012-04-06 19:50:29
【问题描述】:

我想将数据保存在 cookie 中(用户名、电子邮件地址等),但我不想让用户轻松阅读或修改它。我需要能够读回数据。我怎么能用 php 5.2+ 做到这一点?

它将用于“欢迎回来鲍勃”类型的功能。它不能替代持久性或会话存储。

【问题讨论】:

    标签: php security encryption cookies


    【解决方案1】:

    我们在项目中使用 mcrypt 来实现加密。以下是基于互联网上的内容的代码示例:

    <?php
    class MyProjCrypt {
    
        private $td;
        private $iv;
        private $ks;
        private $salt;
        private $encStr;
        private $decStr;
    
    
        /**
         *  The constructor initializes the cryptography library
         * @param $salt string The encryption key
         * @return void
         */
        function __construct($salt) {
            $this->td = mcrypt_module_open('rijndael-256', '', 'ofb', ''); // algorithm
            $this->ks = mcrypt_enc_get_key_size($this->td); // key size needed for the algorithm
            $this->salt = substr(md5($salt), 0, $this->ks);
        }
    
        /**
         * Generates a hex string of $src
         * @param $src string String to be encrypted
         * @return void
         */
        function encrypt($src) {
            srand(( double) microtime() * 1000000); //for sake of MCRYPT_RAND
            $this->iv = mcrypt_create_iv($this->ks, MCRYPT_RAND); 
            mcrypt_generic_init($this->td, $this->salt, $this->iv);
            $tmpStr = mcrypt_generic($this->td, $src);
            mcrypt_generic_deinit($this->td);
            mcrypt_module_close($this->td);
    
            //convert the encrypted binary string to hex
            //$this->iv is needed to decrypt the string later. It has a fixed length and can easily 
            //be seperated out from the encrypted String
            $this->encStr = bin2hex($this->iv.$tmpStr);
    
        }
    
        /**
         * Decrypts a hex string    
         * @param $src string String to be decrypted
         * @return void
         */
        function decrypt($src) {
            //convert the hex string to binary
            $corrected = preg_replace("[^0-9a-fA-F]", "", $src);
            $binenc = pack("H".strlen($corrected), $corrected);
    
            //retrieve the iv from the encrypted string
            $this->iv = substr($binenc, 0, $this->ks);
    
            //retrieve the encrypted string alone(minus iv)
            $binstr = substr($binenc, $this->ks);
    
            /* Initialize encryption module for decryption */
            mcrypt_generic_init($this->td, $this->salt, $this->iv);
            /* Decrypt encrypted string */
            $decrypted = mdecrypt_generic($this->td, $binstr);
    
            /* Terminate decryption handle and close module */
            mcrypt_generic_deinit($this->td);
            mcrypt_module_close($this->td);
            $this->decStr = trim($decrypted);
    
        }
    }
    

    【讨论】:

    • IV(初始化向量)是一个随机块,用于在流的前面进行加密。这是为了防止已知明文攻击。您不需要知道解密的 IV,只需要知道它的大小。通常这个尺寸是标准的。
    • 如果没有 IV,我无法找到 mcrypt_generic_init 的方法。
    【解决方案2】:

    我建议您不仅要对数据进行加密,还要对数据进行签名。如果您不签署数据,您将无法可靠地判断用户是否修改了数据。此外,为避免重播,您可能需要在数据中添加一些时间戳/有效期信息。

    【讨论】:

      【解决方案3】:

      如果您不希望您的用户阅读它,请不要将其放入 cookie 中; 取而代之的是使用 Session 的 cookie 来保留更长时间。这样数据就保留在服务器上,而不是用户的计算机上。

      See this article about persistant sessions

      【讨论】:

        【解决方案4】:

        有关加密示例,请参阅http://www.osix.net/modules/article/?id=606 中的“对称加密”部分。

        为防止未经授权的修改,请使用 HMAC:http://php.net/hash-hmac,以及一般的 HMAC:http://en.wikipedia.org/wiki/HMAChttp://en.wikipedia.org/wiki/Message_authentication_code

        如果您不需要,请不要将敏感数据存储在 cookie 中,即使是加密的也不行。您可能想阅读有关“数据间接”的更多信息。

        【讨论】:

          【解决方案5】:

          如果您绝对必须这样做,那么您可以使用mcrypt 中的对称加密功能。

          http://php.net/mcrypt

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-24
            • 2014-01-19
            • 2011-01-21
            • 2012-05-19
            • 1970-01-01
            • 2010-12-03
            • 2016-12-01
            • 2011-10-29
            相关资源
            最近更新 更多