一段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中輸出JS語(yǔ)句以及亂碼問(wèn)題的解決方案
今天小編就為大家分享一篇關(guān)于在PHP中輸出JS語(yǔ)句以及亂碼問(wèn)題的解決方案,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
PHP中include和require的區(qū)別實(shí)例分析
網(wǎng)上太多關(guān)于PHP中include與require區(qū)別。然而事實(shí)真的如此嗎,今天我們就通過(guò)一個(gè)具體的實(shí)例來(lái)簡(jiǎn)單分析驗(yàn)證下2017-05-05
php Memcache 中實(shí)現(xiàn)消息隊(duì)列
Memcache 一般用于緩存服務(wù)。但是很多時(shí)候,比如一個(gè)消息廣播系統(tǒng),需要一個(gè)消息隊(duì)列。直接從數(shù)據(jù)庫(kù)取消息,負(fù)載往往不行。如果將整個(gè)消息隊(duì)列用一個(gè)key緩存到memcache里面.2009-11-11
php阿拉伯?dāng)?shù)字轉(zhuǎn)中文人民幣大寫(xiě)
這篇文章主要介紹了php阿拉伯?dāng)?shù)字轉(zhuǎn)中文大寫(xiě)金額,分享了兩種實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-12-12
Smarty是一個(gè)php模板引擎,它分開(kāi)了邏輯程序和外在的內(nèi)容,提供了一種易于管理的方法. Smarty要求web服務(wù)器運(yùn)行php4.0.6和以上版本. smarty安裝需要smarty庫(kù)文件。可以去官方網(wǎng)站http://smarty.php.net下載。 網(wǎng)上講了很多安裝的教程,但是我都沒(méi)有成功,所以直接把整個(gè)目錄名改為smarty直接復(fù)制到了網(wǎng)站所在的目錄下,然后打開(kāi)http://網(wǎng)站路徑/smarty/demo/index.php,顯示正常,應(yīng)該算是安裝成功了。2008-03-03
PHP項(xiàng)目在Docker(WSL2)中運(yùn)行緩慢的解決方法
最近在使用Docker容器運(yùn)行PHP項(xiàng)目的時(shí)候,發(fā)現(xiàn)特別緩慢,例如一個(gè)干凈的?ThinkPHP?5.1?框架,訪問(wèn)首頁(yè)都需要1秒以上,如果再加上數(shù)據(jù)庫(kù)查詢(xún)、復(fù)雜的業(yè)務(wù)邏輯等代碼的話(huà),那速度可想而知,所以本文就給大家介紹了解決方法,需要的朋友可以參考下2023-09-09

