> php教程 > php手册 > 迅速学会PHP加密解密技巧

迅速学会PHP加密解密技巧

WBOY
풀어 주다: 2016-06-13 11:11:24
원래의
1033명이 탐색했습니다.

我们在这里为大家详细介绍了有关

使用这些PHP加密解密的原因是因为有时自己的URL地址被人获取以后想破解你里面传值的内容就必须知道你的key,没有key,他应该要破了一阵子才能知道你URL里面的内容吧...

闲话少说,先将它们打包成一个文件就叫fun.php吧

  1.  ?php  
  2. function passport_encrypt($txt, $key) {  
  3. srand((double)microtime() * 1000000);  
  4. $encrypt_key = md5(rand(0, 32000));  
  5. $ctr = 0;  
  6. $tmp = '';  
  7. for($i = 0;$i  strlen($txt); $i++) {  
  8. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;  
  9. $tmp .= $encrypt_key[$ctr].($txt[$i]
     ^ $encrypt_key[$ctr++]);  
  10. }  
  11. return base64_encode(passport_key($tmp, $key));  
  12. }  
  13. function passport_decrypt($txt, $key) {  
  14. $txt = passport_key(base64_decode($txt), $key);  
  15. $tmp = '';  
  16. for($i = 0;$i  strlen($txt); $i++) {  
  17. $md5 = $txt[$i];  
  18. $tmp .= $txt[++$i] ^ $md5;  
  19. }  
  20. return $tmp;  
  21. }  
  22. function passport_key($txt, $encrypt_key) {  
  23. $encrypt_key = md5($encrypt_key);  
  24. $ctr = 0;  
  25. $tmp = '';  
  26. for($i = 0; $i  strlen($txt); $i++) {  
  27. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;  
  28. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];  
  29. }  
  30. return $tmp;  
  31. }  
  32. ?> 

以下是一些示例…加深对这三个PHP加密解密函数的理解…
 

<ol class="dp-xml">
<li class="alt"><span><span>//string.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>include “fun.php”;  </span></li>
<li>
<span>$</span><span class="attribute">txt</span><span> = “This is a test”;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">key</span><span> = “testkey”;  </span>
</li>
<li>
<span>$</span><span class="attribute">encrypt</span><span> = </span><span class="attribute-value">passport_encrypt</span><span>($txt,$key);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">decrypt</span><span> = </span><span class="attribute-value">passport_decrypt</span><span>($encrypt,$key);  </span>
</li>
<li>
<span>echo $txt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span>echo $encrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $decrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
<li><span>//array.php  </span></li>
<li class="alt"><span class="tag"><span> ?php  </span></span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt">
<span>$</span><span class="attribute">array</span><span class="attribute-value">array</span><span> = array(  </span>
</li>
<li>
<span>"a" =</span><span class="tag">></span><span> "1",  </span>
</li>
<li class="alt">
<span>"b" =</span><span class="tag">></span><span> "2",  </span>
</li>
<li>
<span>"c" =</span><span class="tag">></span><span> "3",  </span>
</li>
<li class="alt">
<span>"d" =</span><span class="tag">></span><span> "4"  </span>
</li>
<li><span>);  </span></li>
<li class="alt"><span>//serialize产生一个可存储的值,<br>返回一个字符串,unserialize还原  </span></li>
<li>
<span>$</span><span class="attribute">txt</span><span> = </span><span class="attribute-value">serialize</span><span>($array);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">key</span><span> = “testkey”;  </span>
</li>
<li>
<span>$</span><span class="attribute">encrypt</span><span> = </span><span class="attribute-value">passport_encrypt</span><span>($txt,$key);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">decrypt</span><span> = </span><span class="attribute-value">passport_decrypt</span><span>($encrypt,$key);  </span>
</li>
<li>
<span>$</span><span class="attribute">decryptArray</span><span> = </span><span class="attribute-value">unserialize</span><span>($decrypt);  </span>
</li>
<li class="alt">
<span>echo $txt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $encrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span>echo $decrypt.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li>
<span>echo $decryptArray.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><span class="tag"><span> </span><span class="tag-name">hr</span><span class="tag">></span><span>”;  </span></span></span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
로그인 후 복사

关键的地方来了…当你要跳转到另外一个网址,但又要保证你的session无误的时候,你需要对session作一个处理.貌似一个公司有一个网站又有一个论坛,两个地方都有注册和登录,但又不想让用户在主页登录后跳转到论坛的时候session失效,即是登录一次跑完整间公司…

那要怎样来处理用户的session呢…

网页都是无状态的,如果要在新的网页中继续使用session,则需要把session从一个地方移到另一个地方,可能有些人已经想到了,我可以通过url传址的方式来调用它….而PHP有个处理session的变量,叫$_SESSION.于是….

将需要注册的session转换成一个数组吧.那么,你可以这样写:

<ol class="dp-xml">
<li class="alt"><span><span>//login.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>session_start();  </span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt"><span>….  </span></li>
<li><span>$_SESSION[“userid”];  </span></li>
<li class="alt"><span>$_SESSION[“username”];  </span></li>
<li><span>$_SESSION[“userpwd”];  </span></li>
<li class="alt"><span>…  </span></li>
<li>
<span>header("Location: http:<br>//$domain/process.php?</span><span class="attribute">s</span><span>="<br>.urlencode(passport_encrypt<br>(serialize($_SESSION),"</span><span class="attribute-value">sessionkey</span><span>")));  </span>
</li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
로그인 후 복사

PHP加密解密例子中先用serialize将$_SESSION变成可存储的数据,然后通过passport_encrypt将这个数据加密,加urlencode的原因是因为$_SESSION加密时,有可能会产生像料想不到的编码,所以以防万一…(事实证明非常有效)
处理下先

<ol class="dp-xml">
<li class="alt"><span><span>//process.php  </span></span></li>
<li><span class="tag"><span> ?php  </span></span></li>
<li class="alt"><span>session_start();  </span></li>
<li><span>include “fun.php”;  </span></li>
<li class="alt">
<span>$</span><span class="attribute">_SESSION</span><span>=</span><span class="attribute-value">unserialize</span><span>(passport<br>_decrypt($_GET["s"],"sessionkey"));  </span>
</li>
<li><span>header("Location: http://$domain/index.php");  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
로그인 후 복사

先用$_GET[“s”]获取URL的参数,然后用passport_decrypt将其解密,再用unserialize将其数据还原成原始数据
到了这步处理,你的网页就可能通过header自由跳转啦….

这种PHP加密解密方法还涉及到安全性的问题,如果你的url地址在传址的过程中被人家获取的话,那就真的是不好意思了…人家虽然可能破解不了url里边的内容,但人家也可以直接用这个url地址来登录你的一些个人账户啊,邮箱帐户啊甚至银行帐户(当然很少人会这样写,我例外,哈哈)…听起来好怕….但其实你可以在跳转页面作取消session处理….

以下是加强版的process.php

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li><span>session_start();  </span></li>
<li class="alt"><span>include_once "fun.php";  </span></li>
<li>
<span>$</span><span class="attribute">_SESSION</span><span>=</span><span class="attribute-value">unserialize</span><span>(passport_<br>decrypt($_GET["s"],"sessionkey"));  </span>
</li>
<li class="alt">
<span>if((time()-$_SESSION["TIME"])</span><span class="tag">></span><span>30){  </span>
</li>
<li><span>header("Location: http://<br>$domain/ login.php");  </span></li>
<li class="alt"><span>unset($_SESSION["USERNAME"]);  </span></li>
<li><span>unset($_SESSION["PASSWORD"]);  </span></li>
<li class="alt"><span>}  </span></li>
<li><span>else  </span></li>
<li class="alt"><span>header("Location: http://<br>$domain/ index.php");  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
로그인 후 복사

写这个PHP加密解密文件之前,你还要在登录那边设置

$_SESSION["TIME"] = time();

设置这个的原因主要是获取两边的时间,如果跳转的时候超过30秒的时候,你就可以让它跳转到login.php登录页面,网速慢的客户就不好意思啦…但这也预防了如果此url被人获取,而这个人又没有在30秒内登录的话,那就不好意思啊,超时重新登录.

$_SESSION["USERNAME"]和$_SESSION["PASSWORD"] 这两个东东就是用户登录时需要输入的用户名和密码了….取消这两个session的原因就是因为如果你的url被人获取了,那个人虽然在超过30秒内跳转到loign.php的页面,但那些传过来的session依然有效,只要将url后缀login.php改为index.php….那他一样登录成功…


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿