仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 775|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网页设计一个双向加密解密法(php)

[复制链接]
跳转到指定楼层
楼主
发表于 2015-2-4 00:31:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
到现在,对排版还是不很熟练,经常会排不好。加密|解密   基于进制转换的
<?
//made by huyang @2005-01-20||11-04-06
##用n进位制到m进位制
##0~9A-Z,最多可了解为36进制局限
print'<title>加密解密法</title>';
class carry
  {
    function carry($n,$m)
      {
$this->n=$n;
$this->m=$m;
$this->chn['0']=0;
$this->chn['1']=1;
$this->chn['2']=2;
$this->chn['3']=3;
$this->chn['4']=4;
        $this->chn['5']=5;
        $this->chn['6']=6;
        $this->chn['7']=7;
        $this->chn['8']=8;
        $this->chn['9']=9;
        $this->chn['A']=10;
        $this->chn['B']=11;
        $this->chn['C']=12;
        $this->chn['D']=13;
        $this->chn['E']=14;
        $this->chn['F']=15;
        $this->chn['G']=16;
        $this->chn['H']=17;
        $this->chn['I']=18;
        $this->chn['J']=19;
        $this->chn['K']=20;
        $this->chn['L']=21;
        $this->chn['M']=22;
        $this->chn['N']=23;
        $this->chn['O']=24;
        $this->chn['P']=25;
        $this->chn['Q']=26;
        $this->chn['R']=27;
        $this->chn['S']=28;
        $this->chn['T']=29;
        $this->chn['U']=30;
        $this->chn['V']=31;
        $this->chn['W']=32;
        $this->chn['X']=33;
        $this->chn['Y']=34;
        $this->chn['Z']=35;
        $this->cn[0]='0';
        $this->cn[1]='1';
        $this->cn[2]='2';
        $this->cn[3]='3';
        $this->cn[4]='4';
        $this->cn[5]='5';
        $this->cn[6]='6';
        $this->cn[7]='7';
        $this->cn[8]='8';
        $this->cn[9]='9';
        $this->cn[10]='A';
        $this->cn[11]='B';
        $this->cn[12]='C';
        $this->cn[13]='D';
        $this->cn[14]='E';
        $this->cn[15]='F';
        $this->cn[16]='G';
        $this->cn[17]='H';
        $this->cn[18]='I';
        $this->cn[19]='J';
        $this->cn[20]='K';
        $this->cn[21]='L';
        $this->cn[22]='M';
        $this->cn[23]='N';
        $this->cn[24]='O';
        $this->cn[25]='P';
        $this->cn[26]='Q';
        $this->cn[27]='R';
        $this->cn[28]='S';
        $this->cn[29]='T';
        $this->cn[30]='U';
        $this->cn[31]='V';
        $this->cn[32]='W';
        $this->cn[33]='X';
        $this->cn[34]='Y';
        $this->cn[35]='Z';
      }
    function check1($a)
      {
if(ereg("[0-9]",$a))
   if($a>=2)
     if($a<=36)
       return true;
return false;
      }
    function check2($a)
      {
$la=0;
for($j=0;$j<strlen($a);$j++)
   {
     if($this->chn[substr($a,$j,1)]>=$this->n)
       $la=1;
     if($la)
       break;
   }
if($la)
   return false;
else
   return true;
      }
    function toDEC($a)//$n->十进制
      {
if($this->n==10)
   return $a;
else
   {
     $a=strrev($a);
     $k=0;
     for($i=0;$i<strlen($a);$i++)
       {
  $k+=$this->chn[substr($a,$i,1)]*pow($this->n,$i);
       }
     return $k;
   }
      }
    function gethigh($a)
      {
if($a<=1)
   return 1;
else
   {
     for($i=0;$i<100;$i++)
       {
  if(pow($this->m,$i)>$a)
    break;
       }
     return $i;
   }
      }
    function toM($a)//十进制->$m
      {
$a1=$this->gethigh($a);
$res="";
for($i=1;$i<=$a1;$i++)
   {
     $u=($a-$a%pow($this->m,($a1-$i)))/(pow($this->m,($a1-$i)));
     $a-=$u*pow($this->m,($a1-$i));
     $res.=$this->cn[$u];
   }
return $res;
      }
    function get($a)
      {
if(!$this->n)$this->n=10;
if(!$this->m)$this->m=10;
if(($this->check1($this->n))&&($this->check1($this->m)))
   {
     if(ereg("[0-9A-Z]",$a))
       {
  if($this->check2($a))
    {
      return $this->toM($this->toDEC($a));
    }
  else
    return false;
       }
     else
       return false;
   }
else
   return false;
      }
  }
class han
  {
    function han()
      {
for($i=0;$i<10;$i++)
   {
     $this->hu1[$i]=$i;
     $this->hu2[$i]=$i;
   }
for($i=10;$i<36;$i++)
   {
     $this->hu1[$i]=chr($i+55);
     $this->hu2[chr($i+55)]=$i;
   }
for($i=36;$i<62;$i++)
   {
     $this->hu1[$i]=chr($i+61);
     $this->hu2[chr($i+61)]=$i;
   }
$this->hu1[62]=chr(42);
$this->hu1[63]=chr(43);
$this->hu2[chr(42)]=62;
$this->hu2[chr(43)]=63;
      }
    function tocode($str)//将一组字符改变成为代码0~65536
      {
$huyang1=new carry(10,33);
$j=0;
        for($i=0;$i<strlen($str);$i++)
   {
            $p=ord(substr($str,$i,1));
            if($p>160)
       {
                $q=ord(substr($str,++$i,1));
                $p=$p*256+$q;
              }
            else
       $p=255*256+$p;
     $ret.=$huyang1->get($p);
     $j++;
          }
        return $ret;
      }
    function getcode($str)
      {
$huyang=new carry(33,10);
$res="";
for($i=0;$i<(strlen($str)/4);$i++)
   {
     $a=$huyang->get(substr($str,($i*4),4));
     $a2=$a%256;
     $a1=($a-$a2)/256;
     if($a1==255)
       $res.=chr($a2);
     else
       $res.=chr($a1).chr($a2);
   }
return $res;
      }
    function encode($str)
      {
$res="";
$huyang=new carry(35,7);
$huyang1=new carry(8,10);
$str1=$this->tocode($str);
$k=ceil(strlen($str1)/3);
for($i=0;$i<$k;$i++)
   {
     $word=$this->hu1[rand(0,61)];
     $res1=$huyang1->get($huyang->get(substr($str1,($i*3),3)));
     $res11=($res1-$res1%(64*64))/(64*64);
     $res1-=$res11*64*64;
     $res12=($res1-$res1%64)/64;
     $res1-=$res12*64;
     $res13=$res1;
     if($i==($k-1))
       {
  if($res11!=0)
    {
      $res.=$this->hu1[$res11].$this->hu1[$res12].$this->hu1[$res13].$word;
    }
  elseif($res12!=0)
    {
                    $res.=$this->hu1[$res12].$this->hu1[$res13].$word;
    }
  elseif($res13!=0)
    {
                    $res.=$this->hu1[$res13].$word;
    }
       }
     else
       $res.=$this->hu1[$res11].$this->hu1[$res12].$this->hu1[$res13].$word;
   }
return trim($res);
      }
    function discode($str)
      {
$len=ceil(strlen($str)/4);
$res="";
$a=strlen($str)-4*($len-1);
for($i=0;$i<$len;$i++)
   {
     if($i!=($len-1))
       {
         $res1=substr($str,$i*4,1);
         $res2=substr($str,($i*4+1),1);
         $res3=substr($str,($i*4+2),1);
         $res11=$this->hu2[$res1];
         $res12=$this->hu2[$res2];
         $res13=$this->hu2[$res3];
         $res14=$res11*64*64+$res12*64+$res13;
         $res.=" ".$res14;
       }
     else
       {
  if($a%4==0)
    {
                    $res1=substr($str,$i*4,1);
             $res2=substr($str,($i*4+1),1);
             $res3=substr($str,($i*4+2),1);
                    $res11=$this->hu2[$res1];
             $res12=$this->hu2[$res2];
             $res13=$this->hu2[$res3];
             $res14=$res11*64*64+$res12*64+$res13;
             $res.=" ".$res14;
    }
  elseif($a%4==2)
    {
                    $res1=substr($str,$i*4,1);
             $res11=$this->hu2[$res1];
             $res14=$res11;
             $res.=" ".$res14;
    }
  elseif($a%4==3)
    {
                    $res1=substr($str,$i*4,1);
             $res2=substr($str,($i*4+1),1);
             $res11=$this->hu2[$res1];
             $res12=$this->hu2[$res2];
             $res14=$res11*64+$res12;
             $res.=" ".$res14;
    }
       }
   }
return trim($res);
      }
    function decode($str)
      {
$str=$this->discode($str);
$a=explode(" ",$str);
$res="";
$huyang=new carry(7,35);
$huyang1=new carry(10,8);
for($i=0;$i<count($a);$i++)
   {
            //$res.=$huyang->get($a[$i]);
            if($i==(count($a)-1))
       $res.=$huyang->get($huyang1->get($a[$i]));
     else
       {
  $b=$huyang->get($huyang1->get($a[$i]));
  if(strlen($b)==0)
    $res.="000";
  elseif(strlen($b)==1)
    $res.="00".$b;
  elseif(strlen($b)==2)
    $res.="0".$b;
  else
    $res.=$b;
       }
   }
return $this->getcode($res);
      }
  }
$s=microtime();
$st=explode(' ',$s);
$st1=$st[0];
$st2=$st[1];
$huyang=new han;
$str=$_POST['str'];
if(!$str)$str="请输出查询语句!!";
$len=strlen($str);
if($submit)
  $a=$huyang->encode($str);
if($submit1)
  $a=$huyang->decode($str);
$a=str_replace("\\","\",$a);
$a=str_replace("\\"","\"",$a);
$a=str_replace("\\'","\'",$a);
$s=microtime();
$st=explode(' ',$s);
$sta=$st[0];
$stb=$st[1];
$ss1=$sta-$st1;
$ss2=$stb-$st2;
$cus=$ss1+$ss2;
$cus1=$cus/$len;
?>
<form method="POST" action="carry.php">
  <table border="1" cellpadding="0" cellspacing="0" width="800">
  <tr>
      <td width="9%" height="16"><b>消耗工夫/字长-(均匀耗时)</b></td>
      <td width="91%" height="16"><?echo $cus.'/'.$len.'--('.$cus1.')';?></td>
    </tr>
    <tr>
      <td width="9%" height="16"><b>原始语句</b></td>
      <td width="91%" height="16"><textarea cols="87" rows="8" readonly ><?echo $str;?></textarea></td>
    </tr>
    <tr>
      <td width="9%" height="16"><b>了局</b></td>
      <td width="91%" height="16"><textarea cols="87" rows="8" readonly ><?echo $a;?></textarea></td>
    </tr>
  </table>
  <p><textarea rows="8" name="str" cols="87"><?echo $a;?></textarea></p>
  <p><input type="submit" value="加密" name="submit">
     &nbsp&nbsp<input type="submit" value="解密" name="submit1"></p>
</form>
  对于PHP的语法结构,刚开始真的很不习惯,真搞不懂为什么每个变量之前都要加个“$”符号,每个语句写完之后都必须加上“分号”来表示此句已经结束,还有,PHP对字母的大小写是敏感的,写的时候一定要注意大小写的区别。
谁可相欹 该用户已被删除
沙发
发表于 2015-2-4 13:17:14 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
变相怪杰 该用户已被删除
板凳
发表于 2015-2-4 13:33:37 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
金色的骷髅 该用户已被删除
地板
发表于 2015-2-21 20:49:29 | 只看该作者
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
活着的死人 该用户已被删除
5#
发表于 2015-2-22 11:31:53 | 只看该作者
你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。
海妖 该用户已被删除
6#
发表于 2015-3-1 17:25:52 | 只看该作者
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
admin 该用户已被删除
7#
发表于 2015-3-1 21:38:40 | 只看该作者
当然这种网站的会员费就几十块钱。
8#
 楼主| 发表于 2015-3-2 15:09:31 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
分手快乐 该用户已被删除
9#
发表于 2015-3-7 13:10:32 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
爱飞 该用户已被删除
10#
发表于 2015-3-15 06:31:53 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
因胸联盟 该用户已被删除
11#
发表于 2015-3-17 16:11:23 | 只看该作者
本文当是我的笔记啦,遇到的问题随时填充
兰色精灵 该用户已被删除
12#
发表于 2015-3-17 19:09:43 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
莫相离 该用户已被删除
13#
发表于 2015-3-24 20:57:44 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
再现理想 该用户已被删除
14#
发表于 2015-4-1 08:10:10 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
小妖女 该用户已被删除
15#
发表于 2015-4-24 11:34:21 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
灵魂腐蚀 该用户已被删除
16#
发表于 2015-5-4 19:21:20 | 只看该作者
建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。
透明 该用户已被删除
17#
发表于 2015-5-8 15:36:42 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
飘灵儿 该用户已被删除
18#
发表于 2015-6-4 12:30:37 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
小魔女 该用户已被删除
19#
发表于 2015-6-5 13:58:24 | 只看该作者
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-20 19:54

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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