diff --git a/general_code_2013 b/general_code_2013 index a6f989c..ce42791 100644 --- a/general_code_2013 +++ b/general_code_2013 @@ -111,3 +111,46 @@ public function new_rand($rand_arr) { return isset($ret) ? $ret : false; }//End of function + + + + + + /** + * 加密 + * @param string $str 要加密的字符串 + * @param string $key 密钥 + * @return string + * */ + static function encrypt($str, $key) { + if ($key == "" || $str == ""){ + return ""; + } + $result = ""; + for($i = 0;$i < ceil(strlen($str) / strlen($key));$i++) { + $result = $result . bin2hex(substr($str, $i * strlen($key), ($i + 1) * strlen($key)) ^ $key); + } + return $result; + } + + /** + * 解密 + * @param string $str 要解密的字符串 + * @param string $key 密钥 + * @return string + * */ + static function decrypt($str, $key) { + if ($key == "" || $str == ""){ + return ""; + } + $result = ""; + $j = 0; + for($i = 0;$i < strlen($str) / 2;$i++) { + if ($j >= strlen($key)){ + $j = 0; + } + $result = $result . (chr((hexdec(substr($str, $i * 2, 2)))) ^ substr($key, $j, 1)); + $j++; + } + return $result; + } diff --git "a/php\347\253\257\344\270\216\347\247\273\345\212\250\347\253\257\344\272\244\344\272\222" "b/php\347\253\257\344\270\216\347\247\273\345\212\250\347\253\257\344\272\244\344\272\222" new file mode 100644 index 0000000..5a3551a --- /dev/null +++ "b/php\347\253\257\344\270\216\347\247\273\345\212\250\347\253\257\344\272\244\344\272\222" @@ -0,0 +1 @@ +http://blog.snsgou.com/post-767.html