大佬教程收集整理的这篇文章主要介绍了php写的AES加密解密类分享,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
今天写了一个PHP的AES加密类。适用于Yii的扩展。 如果不用在Yii框架中,把代码中Yii::app()->params['encryptKey'] 换成你对应的默认key就可以了。 类代码:
PHP;">
params['encryptKey'] : $key,mcrypt_enc_get_key_size($module));
/* Create the IV and determine the keysize length,use MCRYPT_RAND
* on Windows instead */
$iv = substr(md5($key),mcrypt_enc_get_iv_size($modulE));
/* Intialize encryption */
mcrypt_generic_init($module,$key,$iv);
@H_696_4@ /* Encrypt data */
$encrypted = mcrypt_generic($module,$plaintext);
/* Terminate encryption handler */
mcrypt_generic_deinit($modulE);
mcrypt_module_close($modulE);
return base64_encode($encrypted);
}
/**
* This was AES-128 / CBC / NoPadding 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 <a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a> 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_enc_get_key_size($modulE));
$iv = substr(md5($key),mcrypt_enc_get_iv_size($modulE));
/* Initialize encryption module for decryption */
mcrypt_generic_init($module,$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 rtrim($decrypted,"\0");
}
/**
* Returns the length of the given String.
* If available uses the multibyte String function mb_strlen.
* @param String $String the String being measured for length
* @return Integer the length of the String
*/
private static function strlen($String)
{
return extension_loaded('mbString') ? mb_strlen($String,'8bit') : strlen($String);
}
/**
* Returns the portion of String specified by the start and length parameters.
* If available uses the multibyte String function mb_substr
* @param String $String the input String. Must be one character or longer.
* @param Integer <a href="http://code.js-code.com/tag/start/" target="_blank" class="keywords">$start</a> the starTing position
* @param Integer $length the desired portion length
* @return String the extracted part of String,or falSE on failure or an empty String.
*/
private static function substr($String,<a href="http://code.js-code.com/tag/start/" target="_blank" class="keywords">$start</a>,$length)
{
return extension_loaded('mbString') ? mb_substr($String,$length,'8bit') : substr($String,$length);
}
}
以上是大佬教程为你收集整理的php写的AES加密解密类分享全部内容,希望文章能够帮你解决php写的AES加密解密类分享所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。