[color=red][b]PHP加密解密字符串[/b][/color] [url]http://www.helloweba.com/view-blog-255.html[/url], [b][size=large][color=red]这个很不错.[/color][/size][/b]
[color=red][b]一组PHP可逆加密解密算法[/b][/color] [url]http://www.phper.org.cn/index.php?m=content&a=show&post_id=387[/url], 这个很不错.
[url]http://www.phper.org.cn/?post=133[/url]
加密算法如下:
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;
[url]http://www.thinkphp.cn/code/282.html[/url]
/**
* 通用加密
* @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));
}
标签:加密,key,strArr,解密,str,skey,ord,PHP,data
From: https://blog.51cto.com/u_3871599/6410717