简单有key的加密解密算法
1 define('KEY', '123456'); 2 $data = 'oehpetyFTdYwZ3frZi1KkgtizgQs'; // 被加密信息 3 $encrypt = encrypt($data); 4 $decrypt = decrypt($encrypt); 5 echo $encrypt, "\n", $decrypt; 6 function decrypt($data) { 7 $key = md5(KEY); 8 $x = 0; 9 $data = base64_decode($data); 10 $len = strlen($data); 11 $l = strlen($key); 12 $char = $str = ''; 13 for ($i = 0; $i < $len; $i++) { 14 if ($x == $l) { 15 $x = 0; 16 } 17 $char .= substr($key, $x, 1); 18 $x++; 19 } 20 for ($i = 0; $i < $len; $i++) { 21 if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) { 22 $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1))); 23 } else { 24 $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1))); 25 } 26 } 27 return $str; 28 } 29 30 function encrypt($data) { 31 $key = md5(KEY); 32 $x = 0; 33 $len = strlen($data); 34 $l = strlen($key); 35 $char = $str = ''; 36 for ($i = 0; $i < $len; $i++) { 37 if ($x == $l) { 38 $x = 0; 39 } 40 $char .= $key{$x}; 41 $x++; 42 } 43 for ($i = 0; $i < $len; $i++) { 44 $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256); 45 } 46 return base64_encode($str); 47 }