电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> PHP
一个双向加密解密法(php)-PHP教程,PHP应用
作者:网友供稿 点击:25
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 

基于进制转换的

<?
//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>


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·Windows下的PHP5.0安装配制详解-PHP教程,PHP安装
·PHP在XP下IIS和Apache2服务器上的安装-PHP教程,PHP应用
·最近忙于FTP,好站多多!有好多好东东哦!不敢独享!-PHP教程,PHP基础
·PHP 5.0 Pear安装方法-PHP教程,PHP安装
·PHP开发利器-PRADO 1.6(4)-PHP教程,PHP应用
·Sun Sparc Solaris 2.6 Apache-1.3.12+MySQL-3.23.5+PHP-3.0.15 安装记-PHP教程,PHP应用
·php5学习笔记(转)-PHP教程,PHP应用
·APACHE安装笔记-PHP教程,PHP安装
·PHP.MVC的模板标签系统(四)-PHP教程,PHP应用
·PHP.MVC的模板标签系统(二)-PHP教程,PHP应用

最新文章
·PHP源码-利用 QQWry.Dat 实现 IP 地址高效检索
·Php高手带路--问题汇总解答[2]
·PHPQQ编程(2):取QQ在线状态
·php5手动最简安装方法
·福利彩票幸运号码自动生成器
·PHP开发利器-PRADO 1.6
·在Apache 服务器上启用PHP支持
·Windows2000_pro下安装Apache+PHP4+My
·php文件上传的实现
·PHP开发框架的现状和展望


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号