一段php加密解密的代码参考,希望能够帮到有这方便需求的朋友。一段php加密解密的代码参考,希望能够帮到有这方便需求的朋友。 ?php$key = "This is supposed to be a secret key !!!";function keyED($txt,$encrypt_key){$encrypt_key = md5($encrypt_key);$ctr=0;$tmp = "";for ($i=0;$istrlen($txt);$i++){if ($ctr==strlen($encrypt_key)) $ctr=0;$tmp.= substr($t
<?php
$key = "This is supposed to be a secret key !!!";
function keyED($txt,$encrypt_key)
{
$encrypt_key = md5($encrypt_key);
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}
function encrypt($txt,$key)
{
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0;
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($encrypt_key,$ctr,1) .
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
return keyED($tmp,$key);
}
function decrypt($txt,$key)
{
$txt = keyED($txt,$key);
$tmp = "";
for ($i=0;$i<strlen($txt);$i++)
{
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}
$string = "Hello World !!!";
// encrypt $string, and store it in $enc_text
$enc_text = encrypt($string,$key);
// decrypt the encrypted text $enc_text, and store it in $dec_text
$dec_text = decrypt($enc_text,$key);
print "Original text : $string <Br>\n";
print "Encrypted text : $enc_text <Br>\n";
print "Decrypted text : $dec_text <Br>\n";
?>
沃梦达教程
本文标题为:一段php加密解密的代码参考


基础教程推荐
猜你喜欢
- php利用函数将二进制转为字符串的两种方法 2024-12-04
- 基于PHP+MySQL的简单聊天室思路与代码 2022-08-27
- php怎么判断一个数组中的元素是否完全属于另一个数组 2024-12-04
- 掌握php六大关键字 2024-12-04
- PHP实现根据数组的值进行分组 2022-08-01
- php利用函数获取两个数组中不同元素的个数 2024-12-04
- php如何利用函数修改时差 2024-12-05
- php利用for语句和substr()函数怎么实现字符串的反转 2024-12-04
- 前端用PHP写简单的删除接口(三) 2024-12-04
- php基于文本搜索的实例代码 2022-08-22