一段php加密解密的代碼
更新時(shí)間:2007年07月16日 00:00:00 作者:
<?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";
?>
$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";
?>
相關(guān)文章
php正則提取html圖片(img)src地址與任意屬性的方法
下面小編就為大家?guī)硪黄猵hp正則提取html圖片(img)src地址與任意屬性的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02php打開遠(yuǎn)程文件的方法和風(fēng)險(xiǎn)及解決方法
本文講的是php打開遠(yuǎn)程文件的使用方法,還有使用風(fēng)險(xiǎn)和解決方法2013-11-11

PHP基于MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)象持久層的方法
這篇文章主要介紹了PHP基于MySQL數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)象持久層的方法,實(shí)例分析了php實(shí)現(xiàn)持久層的相關(guān)技巧,需要的朋友可以參考下
2015-06-06 
php連接oracle數(shù)據(jù)庫(kù)的方法(測(cè)試成功)
這篇文章主要介紹了php連接oracle數(shù)據(jù)庫(kù)的方法,簡(jiǎn)單分析了php連接Oracle數(shù)據(jù)庫(kù)的常見方法與具體操作技巧,并對(duì)可能出現(xiàn)的問題進(jìn)行了總結(jié)分析,需要的朋友可以參考下
2016-05-05 
詳談php ip2long 出現(xiàn)負(fù)數(shù)的原因及解決方法
下面小編就為大家?guī)硪黄斦刾hp ip2long 出現(xiàn)負(fù)數(shù)的原因及解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
2017-04-04 
PHP簡(jiǎn)單實(shí)現(xiàn)多維數(shù)組合并與排序功能示例
這篇文章主要介紹了PHP簡(jiǎn)單實(shí)現(xiàn)多維數(shù)組合并與排序功能,涉及php數(shù)組合并、排序等相關(guān)操作及array_merge、array_multisort等函數(shù)使用技巧,需要的朋友可以參考下
2017-09-09