{"id":336,"date":"2020-08-19T22:37:09","date_gmt":"2020-08-19T14:37:09","guid":{"rendered":"https:\/\/wp.dj47.top\/?p=336"},"modified":"2021-10-20T16:16:17","modified_gmt":"2021-10-20T08:16:17","slug":"php%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8a%a0%e5%af%86%e3%80%81%e8%a7%a3%e5%af%86%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.djlog.cn\/?p=336","title":{"rendered":"PHP\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u52a0\u5bc6\u3001\u89e3\u5bc6\u51fd\u6570"},"content":{"rendered":"<pre class=\"language-php\"><code>\/**\r\n * \u5b57\u7b26\u4e32\u52a0\u5bc6\u3001\u89e3\u5bc6\u51fd\u6570\r\n *\r\n *\r\n * @param string  $txt        \u5b57\u7b26\u4e32\r\n * @param string  $operation  ENCODE\u4e3a\u52a0\u5bc6\uff0cDECODE\u4e3a\u89e3\u5bc6\uff0c\u53ef\u9009\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3aENCODE\uff0c\r\n * @param string  $key        \u5bc6\u94a5\uff1a\u6570\u5b57\u3001\u5b57\u6bcd\u3001\u4e0b\u5212\u7ebf\r\n * @param string  $expiry     \u8fc7\u671f\u65f6\u95f4\r\n * @return    string\r\n *\/\r\nfunction encry_code($string, $operation = 'ENCODE', $key = '', $expiry = 0) {\r\n    $ckey_length = 4;\r\n    $key = md5($key != '' ? $key : config('encry_key'));\r\n    $keya = md5(substr($key, 0, 16));\r\n    $keyb = md5(substr($key, 16, 16));\r\n    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';\r\n \r\n    $cryptkey = $keya . md5($keya . $keyc);\r\n    $key_length = strlen($cryptkey);\r\n \r\n    $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+\/')) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;\r\n    $string_length = strlen($string);\r\n \r\n    $result = '';\r\n    $box = range(0, 255);\r\n \r\n    $rndkey = array();\r\n    for ($i = 0; $i &lt;= 255; $i++) {\r\n        $rndkey[$i] = ord($cryptkey[$i % $key_length]);\r\n    }\r\n \r\n    for ($j = $i = 0; $i &lt; 256; $i++) {\r\n        $j = ($j + $box[$i] + $rndkey[$i]) % 256;\r\n        $tmp = $box[$i];\r\n        $box[$i] = $box[$j];\r\n        $box[$j] = $tmp;\r\n    }\r\n \r\n    for ($a = $j = $i = 0; $i &lt; $string_length; $i++) {\r\n        $a = ($a + 1) % 256;\r\n        $j = ($j + $box[$a]) % 256;\r\n        $tmp = $box[$a];\r\n        $box[$a] = $box[$j];\r\n        $box[$j] = $tmp;\r\n        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));\r\n    }\r\n \r\n    if ($operation == 'DECODE') {\r\n        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() &gt; 0) &amp;&amp; substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {\r\n            return substr($result, 26);\r\n        } else {\r\n            return '';\r\n        }\r\n    } else {\r\n        return $keyc . rtrim(strtr(base64_encode($result), '+\/', '-_'), '=');\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/** * \u5b57\u7b26\u4e32\u52a0\u5bc6\u3001\u89e3\u5bc6\u51fd\u6570 * * * @param string $txt \u5b57\u7b26\u4e32 * @param  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336"}],"version-history":[{"count":0,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}