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实现的超长文本分页显示功能示例

PHP实现的超长文本分页显示功能示例

本文实例讲述了PHP实现的超长文本分页显示功能。分享给大家供大家参考,具体如下: 一、代码 1、index.php <?php if ($_GET['page']=="...

简述php环境搭建与配置

最近在学习PHP,以下是看PHP100视频教程,做的学习笔记,在这里存放以便今后使用。 apache--PHP--DB(mysql) 一、apache:基本工作 ·用户通过浏览器访问服务...

phpmail类发送邮件函数代码

有了phpmail这个类,你就不用愁了。这是个外国人写的一个类,我们就只管“拿来主义”了。下面是基于这个类里面的send()方法写的一个函数: 复制代码 代码如下: function s...

PHP5各个版本的新功能和新特性总结

本文目录:PHP5.2 以前:autoload, PDO 和 MySQLi, 类型约束PHP5.2:JSON 支持PHP5.3:弃用的功能,匿名函数,新增魔术方法,命名空间,后期静态绑定...

php class中public,private,protected的区别以及实例分析

一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等。protected: 受保护类型,用于本类和继承类调用。private:...