1

Cookie にデータ (ユーザー名、電子メール アドレスなど) を保存したいのですが、ユーザーが簡単に読み取ったり変更したりできません。データを読み戻せるようにする必要があります。どうすればphp 5.2+でそれを行うことができますか?

「おかえりボブ」のような機能に使用されます。永続化またはセッション ストレージに代わるものではありません。

4

5 に答える 5

9

プロジェクトで 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);

    }
}
于 2008-10-06T10:02:40.170 に答える
7

データを暗号化するだけでなく署名することをお勧めします。データに署名しないと、ユーザーがデータを変更したかどうかを確実に判断できなくなります。また、リプレイを避けるために、タイムスタンプ/有効期間の情報をデータに追加することもできます。

于 2008-10-06T09:59:13.967 に答える
6

ユーザーに読ませたくない場合は、Cookie に入れないでください。代わりに、長時間保持される Cookie を使用してセッションを使用します。このようにして、データはユーザーのコンピューターではなくサーバーに残ります。

永続セッションについては、この記事を参照してください

于 2008-10-06T09:55:35.627 に答える
3

暗号化の例については、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 に保存しないでください。「データの間接化」についてもっと読みたいと思うかもしれません。

于 2008-10-06T10:00:45.867 に答える
1

どうしてもこれを行う必要がある場合は、 の対称暗号化機能を使用できますmcrypt

http://php.net/mcrypt

于 2008-10-06T09:59:50.673 に答える