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 curl中gzip的压缩性能测试实例分析

本文实例分析了php curl中gzip的压缩性能测试。分享给大家供大家参考,具体如下: 前因: 请求接口次数很多,每日两亿多次,主要是有些接口返回数据量很大高达110KB(为了减少请求...

Window下PHP三种运行方式图文详解

Window下PHP三种运行方式图文详解

PHP能不能成功的在Apache服务器上运行,就看我们如何去配置PHP的运行方式。PHP运行目前为止主要有三种方式: a、以模块加载的方式运行,初学者可能不容易理解,其实就是将PHP集成...

PHP 输出缓冲控制(Output Control)详解

  php 缓冲简介       其实我对php ob 系列印象还是很模糊,具体怎么玩的,还不是很了解,平时curd,确实对...

真正根据utf8编码的规律来进行截取字符串的函数(utf8版sub_str )

复制代码 代码如下: /* * 功能: 作用跟substr一样,除了它不会造成乱码 * 参数: * 返回: */ function utf8_substr( $str , $start...

php中一个有意思的日期逻辑处理

今天处理了一个很小的问题。 需求是这样的,从周一到周日只能看到上周一到上周日的数据。 这里直接从数据库里根据 date 字段查询 范围即可。 但需要PHP生成 开始日期和结束日期。 最开...