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

yipeiwu_com6年前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逐行输出(ob_flush与flush的组合)

ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情...

php输入数据统一类实例

本文实例讲述了php输入数据统一类。分享给大家供大家参考。具体如下: <?php class cls_request{ private $getdata;//存储ge...

PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】

本文实例总结了PHP字符串逆序排列实现方法。分享给大家供大家参考,具体如下: 关于字符串的逆序排列,最简单的使用PHP函数strrev()的测试代码如下: header('Conte...

PHP mail 通过Windows的SMTP发送邮件失败的解决方案

原因必定在WordPress的php邮件发送类中。轻松Google到了原因:原来Windows的SMTP服务不支持Name<address@domain.com>这样的邮件格...

PHP常用排序算法实例小结【基本排序,冒泡排序,快速排序,插入排序】

php三种基础算法:冒泡,插入和快速排序法 $array = array(2,3,5,6,9,8,1); //冒泡排序思想,前后元素比较 function sort_bulldle...