您的位置:首页 > 编程语言 > PHP开发

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

2016-06-13 08:57 676 查看
/**
* 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
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  return public function