• 技术文章 >后端开发 >php教程

    php5数字型字符串加解密代码_php技巧

    2016-05-17 09:37:23原创456
    /* ----------------------------------------------------------------------------
    * 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['//m.sbmmt.com/m/'.$i.'//m.sbmmt.com/m/'] = $this->replacement{$i};
    $this->replacedec['//m.sbmmt.com/m/'.$this->replacement{$i}.'//m.sbmmt.com/m/'] = $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;
    }
    }
    ?>
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:php5 mysql分页实例代码_php技巧 下一篇:Mysql中limit的用法方法详解与注意事项_php技巧
    千万级数据并发解决方案

    相关文章推荐

    • 实例详解怎么用PHP实现简易的MVC框架• 什么是php-fpm?如何进行优化来提升性能?• 消息队列RabbitMQ入门与PHP实例详解• PHP5中Cookie与 Session使用详解_PHP教程• php中的一些数组排序方法分享_PHP教程
    1/1

    PHP中文网