PHP实现的XXTEA加密解密算法示例
作者:cevin 时间:2023-07-13 22:29:49
本文实例讲述了PHP实现的XXTEA加密解密算法。分享给大家供大家参考,具体如下:
<?php
/**
* Xxtea 加密实现类
*/
class xxtea {
private function long2str($v, $w) {
$len = count($v);
$n = ($len -1) << 2;
if ($w) {
$m = $v[$len -1];
if (($m < $n -3) || ($m > $n))
return false;
$n = $m;
}
$s = array ();
for ($i = 0; $i < $len; $i++)
$s[$i] = pack("V", $v[$i]);
return $w ? substr(implode('', $s), 0, $n) : implode('', $s);
}
private function str2long($s, $w) {
$v = unpack("V*", $s . str_repeat("/0", (4 - strlen($s) % 4) & 3));
$v = array_values($v);
if ($w)
$v[count($v)] = strlen($s);
return $v;
}
private function int32($n) {
while ($n >= 2147483648)
$n -= 4294967296;
while ($n <= 2147483649)
$n += 4294967296;
return (int) $n;
}
public function encrypt($str, $key) {
if ($str == '')
return '';
$v = $this->str2long($str, true);
$k = $this->str2long($key, false);
if (count($k) < 4)
for ($i = count($k); $i < 4; $i++)
$k[$i] = 0;
$n = count($v) - 1;
$z = $v[$n];
$y = $v[0];
$delta = 0x9E3779B9;
$q = floor(6 + 52 / ($n +1));
$sum = 0;
while (0 < $q--) {
$sum = $this->int32($sum + $delta);
$e = $sum >> 2 & 3;
for ($p = 0; $p < $n; $p++) {
$y = $v[$p +1];
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$z = $v[$p] = $this->int32($v[$p] + $mx);
}
$y = $v[0];
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$z = $v[$n] = $this->int32($v[$n] + $mx);
}
return $this->long2str($v, false);
}
public function decrypt($str, $key) {
if ($str == '')
return '';
$v = $this->str2long($str, false);
$k = $this->str2long($key, false);
if (count($k) < 4)
for ($i = count($k); $i < 4; $i++)
$k[$i] = 0;
$n = count($v) - 1;
$z = $v[$n];
$y = $v[0];
$delta = 0x9E3779B9;
$q = floor(6 + 52 / ($n +1));
$sum = $this->int32($q * $delta);
while ($sum != 0) {
$e = $sum >> 2 & 3;
for ($p = $n; $p > 0; $p--) {
$z = $v[$p -1];
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$y = $v[$p] = $this->int32($v[$p] - $mx);
}
$z = $v[$n];
$mx = $this->int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ $this->int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
$y = $v[0] = $this->int32($v[0] - $mx);
$sum = $this->int32($sum - $delta);
}
return $this->long2str($v, true);
}
}
//用法测试:
$strDemo = "www.aspxhome.com";
$key = "123456";
$pwd = new Xxtea();
$pwdrel = $pwd->encrypt($strDemo, $key);
echo $pwdrel;
echo "<br/>";
echo $pwd->decrypt($pwdrel, $key);
?>
运行结果:
{���H(�S��7*�u7U
www.aspxhome.com
希望本文所述对大家PHP程序设计有所帮助。
来源:https://blog.csdn.net/yafeikf/article/details/6341703
标签:PHP,XXTEA,加密
0
投稿
猜你喜欢
SQL Server 2005作业设置定时任务
2024-01-27 13:02:59
Python模块的制作方法实例分析
2021-09-06 05:57:26
python3 lambda表达式详解
2021-03-01 20:28:20
关于javascript中伪数组和真数组的一些小秘密
2023-07-02 03:47:47
mysql存储过程 在动态SQL内获取返回值的方法详解
2024-01-19 01:22:53
asp如何判断偶数和奇数?
2010-01-12 20:16:00
Python 从subprocess运行的子进程中实时获取输出的例子
2023-12-24 18:31:10
php获取文件大小的方法
2024-05-13 09:26:15
ASP中页面限权访问的几种方法
2007-12-13 06:53:00
Python异常处理操作实例详解
2023-05-06 11:31:05
Python彻底删除文件夹及其子文件方式
2022-07-27 04:18:53
在ASP.NET 2.0中操作数据之二十三:基于用户对修改数据进行限制
2023-07-12 08:52:20
详解Django框架中的视图级缓存
2021-02-02 02:13:33
Python脚本利用adb进行手机控制的方法
2023-01-31 20:49:47
asp sqlserver 执行存储过程返回记录集报对象关闭时不允许操作
2011-03-11 10:57:00
详解小白之KMP算法及python实现
2022-08-29 09:09:17
Python写的一个简单DNS服务器实例
2021-03-25 21:13:47
使用c#构造date数据类型
2024-01-15 22:19:15
Python win32com 操作Exce的l简单方法(必看)
2022-12-04 10:20:36
Python实现抓取城市的PM2.5浓度和排名
2023-07-01 11:13:33