[李景山php]每天laravel-20160828|McryptEncrypter-2

    /**
     * Determine if the given key and cipher combination is valid.
     *
     * @param  string  $key
     * @param  string  $cipher
     * @return bool
     */
    public static function supported($key, $cipher)
    {// check key and cipher is valid
        return defined('MCRYPT_RIJNDAEL_128') &&
                ($cipher === MCRYPT_RIJNDAEL_128 || $cipher === MCRYPT_RIJNDAEL_256);
    }// if defined this 128 so we can use it

    /**
     * Encrypt the given value.
     *
     * @param  string  $value
     * @return string
     *
     * @throws \Illuminate\Contracts\Encryption\EncryptException
     */
    public function encrypt($value)
    {// the big function ,is the encrypt means to this value
        $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());// get the iv by size and randomizer

        $value = base64_encode($this->padAndMcrypt($value, $iv));// use this base64 function to has the true value

        // Once we have the encrypted value we will go ahead base64_encode the input
        // vector and create the MAC for the encrypted value so we can verify its
        // authenticity. Then, we'll JSON encode the data in a "payload" array.
        $mac = $this->hash($iv = base64_encode($iv), $value);// get the mac value

        $json = json_encode(compact('iv', 'value', 'mac'));// get the json string

        if (! is_string($json)) {
            throw new EncryptException('Could not encrypt the data.');
        }// if the value is wrong throw exception

        return base64_encode($json);// the return the base64_encode
    }

    /**
     * Pad and use mcrypt on the given value and input vector.
     *
     * @param  string  $value
     * @param  string  $iv
     * @return string
     */
    protected function padAndMcrypt($value, $iv)
    {
        $value = $this->addPadding(serialize($value));// value be change like value

        return mcrypt_encrypt($this->cipher, $this->key, $value, MCRYPT_MODE_CBC, $iv);// then  return the encrypt value
    }// the value is given value and input vector

    /**
     * Decrypt the given value.
     *
     * @param  string  $payload
     * @return string
     */
    public function decrypt($payload)
    {
        $payload = $this->getJsonPayload($payload);// use the payload get the json payload

        // We'll go ahead and remove the PKCS7 padding from the encrypted value before
        // we decrypt it. Once we have the de-padded value, we will grab the vector
        // and decrypt the data, passing back the unserialized from of the value.
        $value = base64_decode($payload['value']);// first get the payload by base64_decode

        $iv = base64_decode($payload['iv']);//iv use the same method

        return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));// too big function
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值