php7+ 版本
/**
* [AesSecurity aes加密,支持PHP7+]
* 算法模式:ECB
* 密钥长度:128
* 补码方式:PKCS7Padding
* 解密串编码方式:base64/十六进制
*/
class Aes
{
/**
* [encrypt aes加密]
* @param [type] $input [要加密的数据]
* @param [type] $key [加密key]
* @return [type] [加密后的数据]
*/
public static function encrypt($input, $key)
{
$key = self::_sha1prng($key);
$iv = '';
$data = openssl_encrypt($input, 'AES-128-ECB', $key, OPENSSL_RAW_DATA, $iv);
$data = base64_encode($data);
return $data;
}
/**
* [decrypt aes解密]
* @param [type] $sStr [要解密的数据]
* @param [type] $sKey [加密key]
* @return [type] [解密后的数据]
*/
public static function decrypt($sStr, $sKey)
{
$sKey = self::_sha1prng($sKey);
$iv = '';
$decrypted = openssl_decrypt(base64_decode($sStr), 'AES-128-ECB', $sKey, OPENSSL_RAW_DATA, $iv);
return $decrypted;
}
/**
* SHA1PRNG算法
* @param [type] $key [description]
* @return [type] [description]
*/
private static function _sha1prng($key)
{
return substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
}
}
php5+ 版本
/**
* [Aes aes加密,支持PHP5+]
* 算法模式:ECB
* 密钥长度:128
* 补码方式:PKCS5Padding/PKCS7Padding
* 解密串编码方式:base64/十六进制
*/
class Aes {
public static function encrypt($plain, $key)
{
if (trim($key) == '') {
return false;
}
$plain = strval($plain);
$block_size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$padded_data = self::_pkcs5_pad($plain, $block_size);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, self::_sha1prng($key), $padded_data, MCRYPT_MODE_ECB, $iv);
return base64_encode($encrypted);
}
public static function decrypt($cipher, $key)
{
if ( ! is_string($cipher) || trim($key) == '') {
return false;
}
if ($decoded = base64_decode($cipher)) {
$block_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($block_size, MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::_sha1prng($key), $decoded, MCRYPT_MODE_ECB, $iv);
return self::_pkcs5_unpad($decrypted);
}
return false;
}
private static function _sha1prng($key)
{
return substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
}
private static function _pkcs5_pad($text, $block_size)
{
$pad = $block_size - (strlen($text) % $block_size);
return $text . str_repeat(chr($pad), $pad);
}
private static function _pkcs5_unpad($text)
{
$end = substr($text, -1);
$last = ord($end);
$len = strlen($text) - $last;
if (substr($text, $len) == str_repeat($end, $last)) {
return substr($text, 0, $len);
}
return false;
}
private static function _pkcs7_pad($string, $blocksize = 32)
{
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
private static function _pkcs7_unpad($string)
{
$pad = ord ($string {strlen($string) - 1});
if ($pad > strlen($string)) {
return false;
}
if (strspn ($string, chr ($pad), strlen($string) - $pad) != $pad) {
return false;
}
return substr($string, 0, - 1 * $pad);
}
}
代码中的相关算法
算法模式:ECB
密钥长度:128
补码方式:PKCS5Padding
解密串编码方式:base64/十六进制
ECB 模式不需求设置 iv
本人工作比较忙,时间有限,下次再展开讨论~