{"id":779,"date":"2012-04-27T10:23:16","date_gmt":"2012-04-27T02:23:16","guid":{"rendered":"http:\/\/www.poloo.org\/?p=779"},"modified":"2012-06-02T22:24:00","modified_gmt":"2012-06-02T14:24:00","slug":"php%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=779","title":{"rendered":"php\u52a0\u5bc6\u89e3\u5bc6"},"content":{"rendered":"<blockquote><p>&lt;?php<br \/>\n\/\/ \u53c2\u6570\u89e3\u91ca<br \/>\n\/\/ $string\uff1a \u660e\u6587 \u6216 \u5bc6\u6587<br \/>\n\/\/ $operation\uff1aDECODE\u8868\u793a\u89e3\u5bc6,\u5176\u5b83\u8868\u793a\u52a0\u5bc6<br \/>\n\/\/ $key\uff1a \u5bc6\u5319<br \/>\n\/\/ $expiry\uff1a\u5bc6\u6587\u6709\u6548\u671f<br \/>\nfunction authcode($string, $operation = &#8216;DECODE&#8217;, $key = &#8216;xiaogg&#8217;, $expiry = 0) {<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u52a8\u6001\u5bc6\u5319\u957f\u5ea6\uff0c\u76f8\u540c\u7684\u660e\u6587\u4f1a\u751f\u6210\u4e0d\u540c\u5bc6\u6587\u5c31\u662f\u4f9d\u9760\u52a8\u6001\u5bc6\u5319<br \/>\n\u00a0\u00a0\u00a0 $ckey_length = strlen($key);<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u5bc6\u5319<br \/>\n\u00a0\u00a0\u00a0 $key = md5($key);<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u5bc6\u5319a\u4f1a\u53c2\u4e0e\u52a0\u89e3\u5bc6<br \/>\n\u00a0\u00a0\u00a0 $keya = md5(substr($key, 0, 16));<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u5bc6\u5319b\u4f1a\u7528\u6765\u505a\u6570\u636e\u5b8c\u6574\u6027\u9a8c\u8bc1<br \/>\n\u00a0\u00a0\u00a0 $keyb = md5(substr($key, 16, 16));<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u5bc6\u5319c\u7528\u4e8e\u53d8\u5316\u751f\u6210\u7684\u5bc6\u6587<br \/>\n\u00a0\u00a0\u00a0 $keyc = $ckey_length ? ($operation == &#8216;DECODE&#8217; ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : &#8221;;<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u53c2\u4e0e\u8fd0\u7b97\u7684\u5bc6\u5319<br \/>\n\u00a0\u00a0\u00a0 $cryptkey = $keya.md5($keya.$keyc);<br \/>\n\u00a0\u00a0\u00a0 $key_length = strlen($cryptkey);<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u660e\u6587\uff0c\u524d10\u4f4d\u7528\u6765\u4fdd\u5b58\u65f6\u95f4\u6233\uff0c\u89e3\u5bc6\u65f6\u9a8c\u8bc1\u6570\u636e\u6709\u6548\u6027\uff0c10\u523026\u4f4d\u7528\u6765\u4fdd\u5b58$keyb(\u5bc6\u5319b)\uff0c\u89e3\u5bc6\u65f6\u4f1a\u901a\u8fc7\u8fd9\u4e2a\u5bc6\u5319\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u5982\u679c\u662f\u89e3\u7801\u7684\u8bdd\uff0c\u4f1a\u4ece\u7b2c$ckey_length\u4f4d\u5f00\u59cb\uff0c\u56e0\u4e3a\u5bc6\u6587\u524d$ckey_length\u4f4d\u4fdd\u5b58 \u52a8\u6001\u5bc6\u5319\uff0c\u4ee5\u4fdd\u8bc1\u89e3\u5bc6\u6b63\u786e<br \/>\n\u00a0\u00a0\u00a0 $string = $operation == &#8216;DECODE&#8217; ? base64_decode(substr($string, $ckey_length)) : sprintf(&#8216;%010d&#8217;, $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;<br \/>\n\u00a0\u00a0\u00a0 $string_length = strlen($string);<br \/>\n\u00a0\u00a0\u00a0 $result = &#8221;;<br \/>\n\u00a0\u00a0\u00a0 $box = range(0, 255);<br \/>\n\u00a0\u00a0\u00a0 $rndkey = array();<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u4ea7\u751f\u5bc6\u5319\u7c3f<br \/>\n\u00a0\u00a0\u00a0 for($i = 0; $i &lt;= 255; $i++) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $rndkey[$i] = ord($cryptkey[$i % $key_length]);<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u7528\u56fa\u5b9a\u7684\u7b97\u6cd5\uff0c\u6253\u4e71\u5bc6\u5319\u7c3f\uff0c\u589e\u52a0\u968f\u673a\u6027\uff0c\u597d\u50cf\u5f88\u590d\u6742\uff0c\u5b9e\u9645\u4e0a\u5bf9\u5e76\u4e0d\u4f1a\u589e\u52a0\u5bc6\u6587\u7684\u5f3a\u5ea6<br \/>\n\u00a0\u00a0\u00a0 for($j = $i = 0; $i &lt; 256; $i++) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $j = ($j + $box[$i] + $rndkey[$i]) % 256;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $tmp = $box[$i];<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $box[$i] = $box[$j];<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $box[$j] = $tmp;<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 \/\/ \u6838\u5fc3\u52a0\u89e3\u5bc6\u90e8\u5206<br \/>\n\u00a0\u00a0\u00a0 for($a = $j = $i = 0; $i &lt; $string_length; $i++) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $a = ($a + 1) % 256;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $j = ($j + $box[$a]) % 256;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $tmp = $box[$a];<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $box[$a] = $box[$j];<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $box[$j] = $tmp;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u4ece\u5bc6\u5319\u7c3f\u5f97\u51fa\u5bc6\u5319\u8fdb\u884c\u5f02\u6216\uff0c\u518d\u8f6c\u6210\u5b57\u7b26<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 if($operation == &#8216;DECODE&#8217;) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ substr($result, 0, 10) == 0 \u9a8c\u8bc1\u6570\u636e\u6709\u6548\u6027<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ substr($result, 0, 10) &#8211; time() &gt; 0 \u9a8c\u8bc1\u6570\u636e\u6709\u6548\u6027<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) \u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u9a8c\u8bc1\u6570\u636e\u6709\u6548\u6027\uff0c\u8bf7\u770b\u672a\u52a0\u5bc6\u660e\u6587\u7684\u683c\u5f0f<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if((substr($result, 0, 10) == 0 || substr($result, 0, 10) &#8211; time() &gt; 0) &amp;&amp; substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return substr($result, 26);<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return &#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br \/>\n\u00a0\u00a0\u00a0 } else {<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u628a\u52a8\u6001\u5bc6\u5319\u4fdd\u5b58\u5728\u5bc6\u6587\u91cc\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u540c\u6837\u7684\u660e\u6587\uff0c\u751f\u4ea7\u4e0d\u540c\u5bc6\u6587\u540e\u80fd\u89e3\u5bc6\u7684\u539f\u56e0<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u56e0\u4e3a\u52a0\u5bc6\u540e\u7684\u5bc6\u6587\u53ef\u80fd\u662f\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u590d\u5236\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u4e22\u5931\uff0c\u6240\u4ee5\u7528base64\u7f16\u7801<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $keyc.str_replace(&#8216;=&#8217;, &#8221;, base64_encode($result));<br \/>\n\u00a0\u00a0\u00a0 }<br \/>\n}<br \/>\necho authcode(&#8220;123456&#8243;,&#8221;jiami&#8221;);<br \/>\necho &#8220;&lt;br&gt;&#8221;;<br \/>\necho authcode(&#8220;801dbc\/enq3NuSJeAjRWs8eVv099h\/OB46xIsCmlVJynWq\/3w&#8221;);<br \/>\n?&gt;<\/p>\n<p>&nbsp;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/\/ \u53c2\u6570\u89e3\u91ca \/\/ $string\uff1a \u660e\u6587 \u6216 &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[36,63,99,101,100],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-Php","tag-php-2","tag-63","tag-99","tag-101","tag-100"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=779"}],"version-history":[{"count":4,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":782,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/779\/revisions\/782"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}