php5.5中有更為可靠和方便的加密方式。喜歡鑽研的朋友可以了解一下:
password_hash()
http://www.php.net/manual/zh/function.password-hash.php
基於mcrypt擴展,按位異或總結的兩個字符串加密解密算法
<?php /** * @info 字符串加密解密算法一,利用mcrypt擴展 * @param string $string 待處理字符串 * $action ENCODE,加密 | DECODE,解密 * @return string $returnstr * @date 2014/4/22 * @author tonglei */ function mcrypt_handle_string($string, $action = 'ENCODE') { !is_array($string) or exit; $action == 'DECODE' && $string = base64_decode($string); $key = "123456"; //key可自定義或在配置文件中獲取 $mcryptAlgorithm = MCRYPT_DES; //選擇一種加密算法 $mcryptMode = MCRYPT_MODE_ECB; //選擇一種加密模式 $mcryptIv = mcrypt_create_iv(mcrypt_get_iv_size($mcryptAlgorithm, $mcryptMode), MCRYPT_RAND); //創建初始化向量 $returnstr = base64_encode(mcrypt_encrypt($mcryptAlgorithm, $key, $string, $mcryptMode, $mcryptIv)); if('DECODE' == $action) { $returnstr = mcrypt_decrypt($mcryptAlgorithm, $key, $string, $mcryptMode, $mcryptIv); } return $returnstr; }
<?php /** * * @info 字符串加密解密算法二 利用按位異或 * @param string $string 待處理字符串 * @param $action ENCODE 加密 | DECODE 解密 * @return string */ function StrCode($string, $action = 'ENCODE') { $action != 'ENCODE' && $string = base64_decode($string); $code = ''; $key = substr(md5($GLOBALS['pwServer']['HTTP_USER_AGENT'] . $GLOBALS['db_hash']), 8, 18); $keyLen = strlen($key); $strLen = strlen($string); for ($i = 0; $i < $strLen; $i++) { $k = $i % $keyLen; $code .= $string[$i] ^ $key[$k]; } return ($action != 'DECODE' ? base64_encode($code) : $code); }