### 一、可逆加密算法 1、PHP加密 class encryptCalss { var $key = 12; function encode($txt) { for($i=0;$ikey); } return $txt=urlencode(base64_encode(urlencode($txt))); } function decode($txt) { $txt = urldecode(base64_decode($txt)); for($i=0;$ikey); } return $txt; } } ?> 2、discuz加密解密 0)&&substr($result,10,16)==substr(md5(substr($result,26).$keyb),0,16)){ returnsubstr($result,26); }else{ return''; } }else{ return$keyc.str_replace('=','',base64_encode($result)); } } ?> 二、PHP的内置函数crypt "; //不安全 echo "crypt加密后".crypt($pass).""; // 比较乱的密码 刷新后还会变 echo "crypt复杂加密后".crypt($pass,substr($pass,0,2)).""; //还是不爽 echo "无敌加密后".md5(crypt($pass,substr($pass,0,2))).""; // 现在让黑客如何破这个密码??? ?> 三、可逆加密 ```php /** * aes加密 * @param $input 待加密字符串 */ public static function aesEncrypt($input) { $key = self::random(16,'1234567890abcdefghijklmnopqrstuvwxyz');//随机生成16位key $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);//128位时为16 $pad = $size - (strlen($input) % $size);//取得补码的长度 $input = $input . str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段 $data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $input , MCRYPT_MODE_ECB); $data = base64_encode($data); //key拼在字符串前 $data = $key.$data; return $data; } /** * aes 解密 * @param $sStr 待解密字符串 */ public static function aesDecrypt($sStr) { //取出前16位为key $sKey = substr($sStr, 0, 16); $sStr = substr($sStr,16); $decrypted= mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$sKey,base64_decode($sStr),MCRYPT_MODE_ECB); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s-1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; } /** * 随机生成key * @param int $length * @param string $chars * @return string */ public static function random($length, $chars = '1234567890') { $hash = ''; $max = strlen($chars) - 1; for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } ```