PHP實(shí)現(xiàn)的XXTEA加密解密算法示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的XXTEA加密解密算法。分享給大家供大家參考,具體如下:
<?php /** * Xxtea 加密實(shí)現(xiàn)類 */ 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.dbjr.com.cn"; $key = "123456"; $pwd = new Xxtea(); $pwdrel = $pwd->encrypt($strDemo, $key); echo $pwdrel; echo "<br/>"; echo $pwd->decrypt($pwdrel, $key); ?>
運(yùn)行結(jié)果:
{���H(�S��7*�u7U
www.dbjr.com.cn
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
在線RSA加密/解密工具:
http://tools.jb51.net/password/rsa_encode
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php加密方法總結(jié)》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》及《php正則表達(dá)式用法總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- 六種php加密解密方法實(shí)例講解
- PHP rsa加密解密算法原理解析
- 基于PHP實(shí)現(xiàn)解密或加密Cloudflar郵箱保護(hù)
- php中加密解密DES類的簡單使用方法示例
- php的RSA加密解密算法原理與用法分析
- RSA實(shí)現(xiàn)JS前端加密與PHP后端解密功能示例
- Js通過AES加密后PHP用Openssl解密的方法
- PHP實(shí)現(xiàn)的AES雙向加密解密功能示例【128位】
- PHP實(shí)現(xiàn)基于3DES算法加密解密字符串示例
- PHP實(shí)現(xiàn)的AES加密、解密封裝類與用法示例
- PHP實(shí)現(xiàn)的DES加密解密類定義與用法示例
- 基于PHP RSA密文過長加密解密 越過1024的解決方法
- PHP的RSA加密解密方法以及開發(fā)接口使用
- PHP使用自定義key實(shí)現(xiàn)對數(shù)據(jù)加密解密的方法
- php實(shí)現(xiàn)的三個常用加密解密功能函數(shù)示例
- PHP代碼加密和擴(kuò)展解密實(shí)戰(zhàn)
相關(guān)文章
php使用Swoole與WebSocket實(shí)現(xiàn)彈幕效果的示例代碼
在本文中,我們將深入探討如何使用Swoole與WebSocket結(jié)合,實(shí)現(xiàn)彈幕效果,并著重強(qiáng)調(diào)需要注意的關(guān)鍵地方,以確保我們的彈幕系統(tǒng)能夠高效、穩(wěn)定地運(yùn)行,感興趣的朋友可以參考下2024-02-02php 求質(zhì)素(素數(shù)) 的實(shí)現(xiàn)代碼
php 求質(zhì)素(素數(shù)) 的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-04-04php中magic_quotes_gpc對unserialize的影響分析
這篇文章主要介紹了php中magic_quotes_gpc對unserialize的影響,以實(shí)例的形式分析了magic_quotes_gpc安全過濾對unserialize造成的影響以及對此的解決方法,非常具有實(shí)用價值,需要的朋友可以參考下2014-12-12PHP函數(shù)篇之掌握ord()與chr()函數(shù)應(yīng)用
ord()函數(shù)把字符轉(zhuǎn)換為十進(jìn)制數(shù)字,chr()函數(shù)把十進(jìn)制數(shù)字轉(zhuǎn)化為字符,在二進(jìn)制,八進(jìn)制,十進(jìn)制與十六進(jìn)制之間充當(dāng)橋梁的作用2011-12-12PHP+AJAX實(shí)現(xiàn)無刷新注冊(帶用戶名實(shí)時檢測)
PHP+AJAX實(shí)現(xiàn)無刷新注冊(帶用戶名實(shí)時檢測)...2007-01-01關(guān)于PHP實(shí)現(xiàn)異步操作的研究
一般來說PHP適用的場合是web頁面展示等耗時比較短的任務(wù),如果對于比較花時間的操作如resize圖片、大數(shù)據(jù)導(dǎo)入、批量發(fā)送EDM、SMS等,就很容易出現(xiàn)操作超時情況2013-02-02