您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Php Aes加密类程序代码分享_PHP教程

2024/4/10 16:11:27发布8次查看
今天没事与了一个php aes加密类程序,适用于yii的扩展如果不用在yii框架中,把代码中yii::app()->params[\'encryptkey\'] 换成你对应的默认key就可以了。
aes加密算法 – 算法原理
aes 算法基于排列和置换运算。排列是对数据重新进行安排,置换是将一个数据单元替换为另一个。aes 使用几种不同的方法来执行排列和置换运算。
aes 是一个迭代的、对称密钥分组的密码,它可以使用128、192 和 256 位密钥,并且用 128 位(16字节)分组加密和解密数据。与公共密钥密码使用密钥对不同,对称密钥密码使用相同的密钥加密和解密数据。通过分组密码返回的加密数据的位数与输入数据相同。迭代加密使用一个循环结构,在该循环中重复置换和替换输入数据。
 代码如下 复制代码
params['encryptkey'] : $key, 0, mcrypt_enc_get_key_size($module));
/* create the iv and determine the keysize length, use mcrypt_rand
* on windows instead */
$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
/* intialize encryption */
mcrypt_generic_init($module, $key, $iv);
/* encrypt data */
$encrypted = mcrypt_generic($module, $plaintext);
/* terminate encryption handler */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return base64_encode(trim($encrypted));
}
/**
* this was aes-128 / cbc / zerobytepadding decrypted.
* @author terry
* @param string $encrypted base64_encode encrypted string
* @param string $key
* @throws cexception
* @return string
*/
public static function aesdecrypt($encrypted, $key = null)
{
if ($encrypted == '') return '';
if(!extension_loaded('mcrypt'))
throw new cexception(yii::t('yii','aesdecrypt requires php mcrypt extension to be loaded in order to use data encryption feature.'));
$ciphertext_dec = base64_decode($encrypted);
$module = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, '');
$key=self::substr($key===null ? yii::app()->params['encryptkey'] : $key, 0, mcrypt_enc_get_key_size($module));
$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
/* initialize encryption module for decryption */
mcrypt_generic_init($module, $key, $iv);
/* decrypt encrypted string */
$decrypted = mdecrypt_generic($module, $ciphertext_dec);
/* terminate decryption handle and close module */
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return self::unpkcs5padding($decrypted);
}
private static function strlen($string)
{
return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
}
private static function substr($string,$start,$length)
{
return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
}
private static function pkcs5padding ($text, $blocksize) {
$pad = $blocksize - (self::strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
private static function unpkcs5padding($text)
{
$pad = ord($text{self::strlen($text)-1});
if ($pad > self::strlen($text)) return false;
if (strspn($text, chr($pad), self::strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
使用方法
 代码如下 复制代码
makekey($key);
$encode = 123456;// 被加密的字符串
$ct = $aes->encryptstring($encode, $keys);
echo encode = .$ct.
;
$cpt = $aes->decryptstring($ct, $keys);
echo decode = .$cpt;
?>
http://www.bkjia.com/phpjc/632843.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/632843.htmltecharticle今天没事与了一个php aes加密类程序,适用于yii的扩展如果不用在yii框架中,把代码中yii::app()->params[\'encryptkey\'] 换成你对应的默认key就可以...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product