设为首页收藏本站language 语言切换
查看: 1478|回复: 0
收起左侧

实例应用:使用PHP来进行加密与解密

[复制链接]
发表于 2010-2-24 13:41:07 | 显示全部楼层 |阅读模式
<>&lt;?php <BR>$key = "This is supposed to be a secret key !!!"; <BR>function keyED($txt,$encrypt_key) <BR>{ <BR>$encrypt_key = md5($encrypt_key); <BR>$ctr=0; <BR>$tmp = ""; <BR>for ($i=0;$i&lt;strlen($txt);$i++) <BR>{ <BR>  if ($ctr==strlen($encrypt_key)) $ctr=0; <BR>  $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1); <BR>  $ctr++; <BR>} <BR>return $tmp; <BR>} <BR>function encrypt($txt,$key) <BR>{ <BR>srand((double)microtime()*1000000); <BR>$encrypt_key = md5(rand(0,32000)); <BR>$ctr=0; <BR>$tmp = ""; <BR>for ($i=0;$i&lt;strlen($txt);$i++) <BR>{ <BR>  if ($ctr==strlen($encrypt_key)) $ctr=0; <BR>  $tmp.= substr($encrypt_key,$ctr,1) . <BR>  (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); <BR>  $ctr++; <BR>} <BR>return keyED($tmp,$key); <BR>} <BR>function decrypt($txt,$key) <BR>{ <BR>$txt = keyED($txt,$key); <BR>$tmp = ""; <BR>for ($i=0;$i&lt;strlen($txt);$i++) <BR>{ <BR>  $md5 = substr($txt,$i,1); <BR>  $i++; <BR>  $tmp.= (substr($txt,$i,1) ^ $md5); <BR>} <BR>return $tmp; <BR>} <BR>$string = "Hello World !!!"; <BR>// encrypt $string,_and_store it in $enc_text <BR>$enc_text = encrypt($string,$key); <BR>// decrypt the encrypted text $enc_text,_and_store it in $dec_text <BR>$dec_text = decrypt($enc_text,$key); <BR>print "Original text : $string &lt;Br&gt;"; <BR>print "Encrypted text : $enc_text &lt;Br&gt;"; <BR>print "Decrypted text : $dec_text &lt;Br&gt;";<BR>?&gt;</P>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-6 07:17 , Processed in 0.413117 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表