ITeye博客

三个超好用的PHP加密解密函数

yd514 · 2013-10-28 16:24 · PHP基础 · 编程语言 · Markdown

三个超好用的PHP加密解密函数,貌似是discuz里的…使用这些加密解密的原因是因为有时自己的URL地址被人获取以后想破解你里面传值的内容就必须知道你的key,没有key,他应该要破了一阵子才能知道你URL里面的内容吧。

将它们打包成一个文件就叫fun.php吧 

复制代码 代码如下:
<?php 
function passport_encrypt($txt, $key) { 
srand((double)microtime() * 1000000); 
$encrypt_key = md5(rand(0, 32000)); 
$ctr = 0; 
$tmp = ''; 
for($i = 0;$i < strlen($txt); $i++) { 
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]); 
} 
return base64_encode(passport_key($tmp, $key)); 
} 

function passport_decrypt($txt, $key) { 
$txt = passport_key(base64_decode($txt), $key); 
$tmp = ''; 
for($i = 0;$i < strlen($txt); $i++) { 
$md5 = $txt[$i]; 
$tmp .= $txt[++$i] ^ $md5; 
} 
return $tmp; 
} 

function passport_key($txt, $encrypt_key) { 
$encrypt_key = md5($encrypt_key); 
$ctr = 0; 
$tmp = ''; 
for($i = 0; $i < strlen($txt); $i++) { 
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++]; 
} 
return $tmp; 
} 
?> 

以下是一些示例加深对这三个加密解密函数的理解 
复制代码 代码如下:
//string.php 
<?php 
include “fun.php”; 

$txt = “This is a test”; 
$key = “testkey”; 
$encrypt = passport_encrypt($txt,$key); 
$decrypt = passport_decrypt($encrypt,$key); 

echo $txt.”<br><hr>”; 
echo $encrypt.”<br><hr>”; 
echo $decrypt.”<br><hr>”; 
?> 

//array.php 
<?php 
include “fun.php”; 

$array = array( 
"a" => "1", 
"b" => "2", 
"c" => "3", 
"d" => "4" 
); 
//serialize产生一个可存储的值,返回一个字符串,unserialize还原 
$txt = serialize($array); 
$key = “testkey”; 
$encrypt = passport_encrypt($txt,$key); 
$decrypt = passport_decrypt($encrypt,$key); 
$decryptArray = unserialize($decrypt); 

echo $txt.”<br><hr>”; 
echo $encrypt.”<br><hr>”; 
echo $decrypt.”<br><hr>”; 
echo $decryptArray.”<br><hr>”; 
?>

来源:http://bbs.php100.com/read-htm-tid-504795.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。