使用PHP来进行加密与解密
上一篇 /
下一篇 2006-11-27 23:50:08
PHPChina 开源社区门户}ATTX(h)z<?phpPHPChina 开源社区门户4y c&[Gn"j.U
$key = "This is supposed to be a secret key !!!";PHPChina 开源社区门户2r5G `&v Y
function keyED($txt,$encrypt_key)PHPChina 开源社区门户.b+DN9KL.LY
{
5H!cF7v on0$encrypt_key = md5($encrypt_key);
^Rj;[.WI!A0$ctr=0;
R'dz ^8X^5e;nr\v0$tmp = "";
*k/S}7c3~:pW^0for ($i=0;$i<strlen($txt);$i++)PHPChina 开源社区门户1ye2@xHc g
{
$i#y\F&nw9a@0 if ($ctr==strlen($encrypt_key)) $ctr=0;PHPChina 开源社区门户&l-C.Q1z'A
vC7t2V
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
/dtH-B Yh0 $ctr++;
1g ~^:iVQEtlo0}PHPChina 开源社区门户3o:v!~pc/P[(L
return $tmp;
Hg|"guJ0}
IduOP"]\C0function encrypt($txt,$key)PHPChina 开源社区门户T)`F&E+XSm
{
0|QZ&]vI_C;a2n0srand((double)microtime()*1000000);
x$XMy2{s
J'};x0$encrypt_key = md5(rand(0,32000));
7[)U[k$U&L \S%`0$ctr=0;
Nvf\,s+f+PDs0$tmp = "";PHPChina 开源社区门户.Y[Q/a5BW!G
for ($i=0;$i<strlen($txt);$i++)
QO(ku d0{PHPChina 开源社区门户kH
iO+f?&P-mHTl
if ($ctr==strlen($encrypt_key)) $ctr=0;PHPChina 开源社区门户$o9sEa$B
$tmp.= substr($encrypt_key,$ctr,1) .PHPChina 开源社区门户0p!e
dMD%G9Xds
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));PHPChina 开源社区门户:o!oD Xpa
$ctr++;
-l,Fd-R6p9\0}PHPChina 开源社区门户q(O!GW4r
return keyED($tmp,$key);
EwfJPb@*z~0}PHPChina 开源社区门户C%Il/wKX$Z*S~
function decrypt($txt,$key)PHPChina 开源社区门户8t8sr'U(C9^5G
{
F'V(n~#t
Dj
X,JR}0$txt = keyED($txt,$key);
PO]p`Uj-r%`1I0$tmp = "";PHPChina 开源社区门户 b5L7D^-~B^B _x*t
for ($i=0;$i<strlen($txt);$i++)PHPChina 开源社区门户|-`@R~6SI!s
{PHPChina 开源社区门户Gj,{d+Jcjga
$md5 = substr($txt,$i,1);
6Ul].K\f8sgW0 $i++;
5mH\,xV7t9|.n#uV5]0 $tmp.= (substr($txt,$i,1) ^ $md5);PHPChina 开源社区门户QW@-ol8Jj"rd
}PHPChina 开源社区门户l:}7C8VE,@#Z
return $tmp;
z({
LlMfV0}
Qc/XfA]4UhV0$string = "Hello World !!!";
u0@EMTx0// encrypt $string, and store it in $enc_text
-@g(oEOv^;Z0$enc_text = encrypt($string,$key);
c@0e:MFKq0// decrypt the encrypted text $enc_text, and store it in $dec_textPHPChina 开源社区门户Pq nT_s;SB
$dec_text = decrypt($enc_text,$key);PHPChina 开源社区门户UHRAlGlnjQ
print "Original text : $string <Br>";PHPChina 开源社区门户/G;g-X3Q8f_Z
print "Encrypted text : $enc_text <Br>";PHPChina 开源社区门户m~1g1HOxCo
print "Decrypted text : $dec_text <Br>";
X.u5Ca&o1Vr0?>
qn3cD0x8pk'r"N9~BNA0
导入论坛
收藏
分享给好友
管理
举报
TAG: