服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php5数字型字符串加解密代码

php5数字型字符串加解密代码

2019-10-29 15:30php教程网 PHP教程

php5数字型字符串加解密代码,对应awk版加解密程序的PHP实现代码

  1. <?php  
  2. /* ----------------------------------------------------------------------------  
  3. * Script Name: encrypt.php  
  4. * Creation Date: 2008-4-7 10:36  
  5. * Last Modified: 2008-4-12 16:00  
  6. * Author: meyu  
  7. * Copyright (c) 2007  
  8. * Purpose: 数字字符串简易加解密  
  9. * ----------------------------------------------------------------------------*/  
  10.  
  11. class Encryption {  
  12.     /**  
  13.      * 最终的密文代码,可设为任意不重复的10位英文字符a-zA-Z  
  14.      */  
  15.     private $replacement = 'urskydMeIV';  
  16.  
  17.     /**  
  18.      * 增加的密文第一位,可设为1位除0以外的整数,即 1-9  
  19.      */  
  20.     private $prefix = "8";  
  21.  
  22.     /**  
  23.      * 公钥,长度小于8位的正整数  
  24.      */  
  25.     private $match = "111111";  
  26.  
  27.     /**  
  28.      * 转换后对照数组  
  29.      */  
  30.     private $replaceenc;  
  31.     private $replacedec;  
  32.  
  33.     function __construct() {  
  34.         for($i =0; $i < 10; $i++) {  
  35.             $this->replaceenc['/'.$i.'/'] = $this->replacement{$i};  
  36.             $this->replacedec['/'.$this->replacement{$i}.'/'] = $i;  
  37.         }  
  38.     }  
  39.  
  40.     public function encrypt($str) {  
  41.         return preg_replace(  
  42.             array_keys($this->replaceenc),  
  43.             $this->replaceenc,  
  44.             $this->mynotin(preg_replace("/(.)(.)/""${2}${1}"$str))  
  45.         );  
  46.     }  
  47.  
  48.     public function decrypt($str) {  
  49.         return preg_replace("/(.)(.)/""${2}${1}",  
  50.             $this->mynotout(preg_replace(array_keys($this->replacedec),$this->replacedec,$str))  
  51.         );  
  52.     }  
  53.  
  54.     private function mynotin($str) {  
  55.         $str_out = "";  
  56.         $i = 0;  
  57.         while(isset($str{7*$i})) {  
  58.             $str_out .= (($this->prefix.substr($str$i*7, 7))+0)^$this->match;  
  59.             $i++;  
  60.         }  
  61.         return $str_out;  
  62.     }  
  63.  
  64.     private function mynotout($str) {  
  65.         $str_out = "";  
  66.         $i = 0;  
  67.         while(isset($str{8*$i})) {  
  68.             $str_out .= substr((substr($str$i*8, 8)+0)^$this->match, 1);  
  69.             $i++;  
  70.         }  
  71.         return $str_out;  
  72.     }  
  73. }  
  74. ?> 

延伸 · 阅读

精彩推荐