找回密码
 注册

QQ登录

只需一步,快速开始

查看: 214|回复: 0

具有时效性的php加密解密函数

[复制链接]
发表于 2014-1-8 10:40:54 | 显示全部楼层 |阅读模式
该函数具有时效性,只要过期就不能解密!通过时间动态加密 加密后数据多样化,增加破解难度
  1. <?php
  2. function encode_pass($tex,$key,$type="encode",$expiry=0){
  3. $chrArr=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  4. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  5. '0','1','2','3','4','5','6','7','8','9');
  6. if($type=="decode"){
  7. if(strlen($tex)<14)return false;
  8. $verity_str=substr($tex, 0,8);
  9. $tex=substr($tex, 8);
  10. if($verity_str!=substr(md5($tex),0,8)){
  11. //完整性验证失败
  12. return false;
  13. }
  14. }
  15. $key_b=$type=="decode"?substr($tex,0,6):$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62].$chrArr[rand()%62];

  16. $rand_key=$key_b.$key;
  17. //设置时间选项
  18. $modnum=0;$modCount=0;$modCountStr="";
  19. if($expiry>0){
  20. if($type=="decode"){
  21. $modCountStr=substr($tex,6,1);
  22. $modCount=$modCountStr=="a"?10:floor($modCountStr);
  23. $modnum=substr($tex,7,$modCount);
  24. $rand_key=$rand_key.(floor((time()-$modnum)/$expiry));
  25. }else{
  26. $modnum=time()%$expiry;
  27. $modCount=strlen($modnum);
  28. $modCountStr=$modCount==10?"a":$modCount;

  29. $rand_key=$rand_key.(floor(time()/$expiry));
  30. }
  31. $tex=$type=="decode"?base64_decode(substr($tex, (7+$modCount))):"xugui".$tex;
  32. }else{
  33. $tex=$type=="decode"?base64_decode(substr($tex, 6)):"xugui".$tex;
  34. }
  35. $rand_key=md5($rand_key);


  36. $texlen=strlen($tex);
  37. $reslutstr="";
  38. for($i=0;$i<$texlen;$i++){
  39. $reslutstr.=$tex{$i}^$rand_key{$i%32};
  40. }
  41. if($type!="decode"){
  42. $reslutstr=trim(base64_encode($reslutstr),"==");
  43. $reslutstr=$modCount?$modCountStr.$modnum.$reslutstr:$reslutstr;
  44. $reslutstr=$key_b.$reslutstr;
  45. $reslutstr=substr(md5($reslutstr), 0,8).$reslutstr;
  46. }else{
  47. if(substr($reslutstr,0, 5)!="xugui"){
  48. return false;
  49. }
  50. $reslutstr=substr($reslutstr, 5);
  51. }
  52. return $reslutstr;
  53. }
  54. $psa=encode_pass("woshi ceshi yong de ","taintainxousad","encode",120);
  55. echo $psa;
  56. echo "\r\n解密:";
  57. echo encode_pass($psa,"taintainxousad",'decode',120);
  58. ?>
复制代码

来自圈子: Demo俱乐部
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-12 03:50 , Processed in 0.017462 second(s), 20 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

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