PHP加密解密代码
PHP加密解密字符串http://www.helloweba.com/view-blog-255.html,这个很不错.
一组PHP可逆加密解密算法http://www.phper.org.cn/index.php?m=content&a=show&post_id=387,这个很不错.
http://www.phper.org.cn/?post=133
加密算法如下:
function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= $key{$x}; $x++; } for ($i = 0; $i < $len; $i++) { $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); } return base64_encode($str); }
解密算法如下:
function decrypt($data, $key) { $key = md5($key); $x = 0; $data = base64_decode($data); $len = strlen($data); $l = strlen($key); for ($i = 0; $i < $len; $i++) { if ($x == $l) { $x = 0; } $char .= substr($key, $x, 1); $x++; } for ($i = 0; $i < $len; $i++) { if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); } else { $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); } } return $str; }
使用:
$data = 'PHP加密解密算法'; // 被加密信息 $key = '123'; // 密钥 $encrypt = encrypt($data, $key); $decrypt = decrypt($encrypt, $key); echo $encrypt, "\n", $decrypt;
http://www.thinkphp.cn/code/282.html
/** * 通用加密 * @param String $string 需要加密的字串 * @param String $skey 加密EKY * @author Anyon Zou <[email protected]> * @date 2013-08-13 19:30 * @return String */ function enCode($string = '', $skey = 'echounion') { $skey = array_reverse(str_split($skey)); $strArr = str_split(base64_encode($string)); $strCount = count($strArr); foreach ($skey as $key => $value) { $key < $strCount && $strArr[$key].=$value; } return str_replace('=', 'O0O0O', join('', $strArr)); } /** * 通用解密 * @param String $string 需要解密的字串 * @param String $skey 解密KEY * @return String * @author Anyon Zou <[email protected]> * @date 2013-08-13 19:30 */ function deCode($string = '', $skey = 'echounion') { $skey = array_reverse(str_split($skey)); $strArr = str_split(str_replace('O0O0O', '=', $string), 2); $strCount = count($strArr); foreach ($skey as $key => $value) { $key < $strCount && $strArr[$key] = rtrim($strArr[$key], $value); } return base64_decode(join('', $strArr)); }
相关推荐
zlfing 2020-04-14
Dimples 2020-04-14
nan00zzu 2020-01-07
codeAB 2019-10-26
风随花落 2015-01-02
阿赞 2013-05-04
hiKirin 2013-05-04
ChinaWin 2013-05-04
wuxiaosi0 2019-06-28
wishy 2019-06-27
whutjiajiao 2019-06-27
草堂 2012-06-27
huangxiaoyu 2015-01-02
清风徐来水波不兴 2018-09-05
81921531 2014-01-10
androidsecurity0 2012-07-04
onegoldensun 2017-08-29