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中用PHPMailer来发送邮件的示例(126.com的例子)

<?phprequire_once('../class.phpmailer.php');$mail= new PHPMailer();$body= "我终于发送邮件成功了!呵呵!g...

PHP使用strtotime计算两个给定日期之间天数的方法

本文实例讲述了PHP使用strtotime计算两个给定日期之间天数的方法。分享给大家供大家参考。具体分析如下: PHP的strtotime函数用于将任何英文文本的日期时间描述解析为Uni...

php生成xml简单实例代码

当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。 尽管XML数据结构的...

微信支付的开发流程详解

最近在公司做了微信支付的接入,这里总结下开发的一些经验 注意,我使用的是微信开放平台的支付,与手机app相关,而与公众账号无关。 微信支付的主要操作流程 1.用户浏览app,选定商品然后...

使用Linux五年积累的一些经验技巧

1. bash中的$相关参数 复制代码 代码如下:$0 - 表示当前文件名  $* - 以空格分离所有参数,形成一个字符串  $@ - 以空格分离所有参数,形成一个字...