Skip to content

Instantly share code, notes, and snippets.

@jacyhao
Last active June 14, 2016 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacyhao/5d441cfeb1f3dac1fb171ae096717ac3 to your computer and use it in GitHub Desktop.
Save jacyhao/5d441cfeb1f3dac1fb171ae096717ac3 to your computer and use it in GitHub Desktop.
<?php
/**
* aes加密解密
*/
class MyAes
{
private $_cipher = MCRYPT_RIJNDAEL_128;
private $_mode = MCRYPT_MODE_CBC;
private $_key;
private $_initializationVectorSize;
public function __construct ($key)
{
$this->_key = $key;
$this->_initializationVectorSize = mcrypt_get_iv_size($this->_cipher, $this->_mode);
}
/**
* 加密
*
* @param String $data 待加密的字符串
* @param Boolean $urlSafe 针对url是否安全
*
* @return String 返回加密后的数据,一般base64编码
**/
public function encrypt ($data, $urlSafe = false)
{
$data = $this->__getEncryptedStr($data);
if ($urlSafe) {
return $this->urlBase64Encode($data);
}
return base64_encode($data);
}
/**
* 解密
*
* @param String $encryptedData 待解密字符串
* @param Boolean $urlSafe 针对url是否安全
*
* @return String 返回解密后的字符串
**/
public function decrypt ($encryptedData, $urlSafe = false)
{
if ($urlSafe) {
$encryptedData = $this->urlBase64Decode($encryptedData);
} else {
$encryptedData = base64_decode($encryptedData);
}
return $this->__getDecryptStr($encryptedData);
}
/**
* 获取加密字符串
*
* @param String $data 待加密的数据
*
* @return String 加密后的数据
**/
private function __getEncryptedStr ($data)
{
$blockSize = mcrypt_get_block_size($this->_cipher, $this->_mode);
$pad = $blockSize - (strlen($data) % $blockSize);
$iv = mcrypt_create_iv($this->_initializationVectorSize, MCRYPT_DEV_URANDOM);
return $iv . mcrypt_encrypt(
$this->_cipher,
$this->_key,
$data . str_repeat(chr($pad), $pad),
$this->_mode,
$iv
);
}
/**
* 获取解密后的字符串
*
* @param String $encryptedData 加密后的字符串
*
* @return String 解密后的字符串
**/
private function __getDecryptStr ($encryptedData)
{
$initializationVector = substr($encryptedData, 0, $this->_initializationVectorSize);
$data = mcrypt_decrypt(
$this->_cipher,
$this->_key,
substr($encryptedData, $this->_initializationVectorSize),
$this->_mode,
$initializationVector
);
$pad = ord($data[strlen($data) - 1]);
return substr($data, 0, -$pad);
}
/**
* urlBase64Encode 生成URL安全的Base64编码
*
* @param string $bin
*
* @access public
* @return string
*/
public function urlBase64Encode ($bin)
{
$base64 = base64_encode($bin);
$base64 = str_replace('+', '-', $base64);
$base64 = str_replace('/', '_', $base64);
$base64 = str_replace('=', '', $base64);
return $base64;
}
/**
* urlBase64Decode 解码URL安全的Base64
*
* @param mixed $base64
*
* @access public
* @return void
*/
public function urlBase64Decode ($base64)
{
$base64 = str_replace('-', '+', $base64);
$base64 = str_replace('_', '/', $base64);
/*
* Put the padding back (a little tricky)
* Base64 has one or two '=' at the end as padding.
* 3 bytes == 4 Base64 chars
* 1 byte left: two B64 chars and two '='
* 2 bytes left: three B64 chars and one '='
*/
$len = strlen($base64);
if ($len % 4 == 2) $base64 .= '==';
elseif ($len % 4 == 3) $base64 .= '=';
return base64_decode($base64);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment