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

yipeiwu_com5年前PHP代码库


<?php
/* ----------------------------------------------------------------------------
* Script Name: encrypt.php
* Creation Date: 2008-4-7 10:36
* Last Modified: 2008-4-12 16:00
* Author: meyu
* Copyright (c) 2007
* Purpose: 数字字符串简易加解密
* ----------------------------------------------------------------------------*/

class Encryption {
    /**
     * 最终的密文代码,可设为任意不重复的10位英文字符a-zA-Z
     */
    private $replacement = 'urskydMeIV';

    /**
     * 增加的密文第一位,可设为1位除0以外的整数,即 1-9
     */
    private $prefix = "8";

    /**
     * 公钥,长度小于8位的正整数
     */
    private $match = "111111";

    /**
     * 转换后对照数组
     */
    private $replaceenc;
    private $replacedec;

    function __construct() {
        for($i =0; $i < 10; $i++) {
            $this->replaceenc['/'.$i.'/'] = $this->replacement{$i};
            $this->replacedec['/'.$this->replacement{$i}.'/'] = $i;
        }
    }

    public function encrypt($str) {
        return preg_replace(
            array_keys($this->replaceenc),
            $this->replaceenc,
            $this->mynotin(preg_replace("/(.)(.)/", "${2}${1}", $str))
        );
    }

    public function decrypt($str) {
        return preg_replace("/(.)(.)/", "${2}${1}",
            $this->mynotout(preg_replace(array_keys($this->replacedec),$this->replacedec,$str))
        );
    }

    private function mynotin($str) {
        $str_out = "";
        $i = 0;
        while(isset($str{7*$i})) {
            $str_out .= (($this->prefix.substr($str, $i*7, 7))+0)^$this->match;
            $i++;
        }
        return $str_out;
    }

    private function mynotout($str) {
        $str_out = "";
        $i = 0;
        while(isset($str{8*$i})) {
            $str_out .= substr((substr($str, $i*8, 8)+0)^$this->match, 1);
            $i++;
        }
        return $str_out;
    }
}
?>

相关文章

PHP中对汉字进行unicode编码和解码的实现方法(必看)

实例如下: //将内容进行UNICODE编码 function unicode_encode($name) { $name = iconv('UTF-8', 'UCS-2', $...

thinkphp 验证码 的使用小结

 thinkphp中的验证码是可以直接调用的,非常方便,我们看一下 Think 文件夹下 有一个名为verify.class.php的文件    首先 我们...

PHP简单实现多维数组合并与排序功能示例

本文实例讲述了PHP简单实现多维数组合并与排序功能。分享给大家供大家参考,具体如下: <?php $record=array( 'title' =>'这个就是...

php unset全局变量运用问题的深入解析

PHP unset全局变量在实际使用中只能销毁局部变量,并不能实现全局变量的目的。下面我们就来具体解决这一问题。希望对大家有所帮助。PHP中语言中有些函数对于初学者来说在字面上是很难理解...

PHP检测一个数组有没有定义的方法步骤

PHP检测一个数组有没有定义的方法步骤

php中定义数组的方法: 1、PHP定义数组的格式: 数组名=array(); 如:$aa=array();//这样就定义了一个数组, 之后给元素赋值: $aa[0]="90...