zoukankan      html  css  js  c++  java
  • PHP版3DES加解密类

    <?php
    /**
    * 
    * PHP版3DES加解密类
    *
    * 可与java的3DES(DESede)加密方式兼容
    *
    * @Author: Luo Hui (farmer.luo at gmail.com)
    *
    * @version: V0.1 2008.12.04
    *
    */
    class ReversableEncrypt
    {    
        public $key    = "Crahafagusw74ecr2meb7edaHezAwrU2";
        public $iv    = "23456789"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };
        
        
        /**
         * 3des加密
         * 
         * @param    string        $input            明文
         * @return string
         */
        public function encrypt($input)
        {
            $input = $this->padding( $input );
            $key = base64_decode($this->key);
            $td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
            //使用MCRYPT_3DES算法,cbc模式
            mcrypt_generic_init($td, $key, $this->iv);
            //初始处理
            $data = mcrypt_generic($td, $input);
            //加密
            mcrypt_generic_deinit($td);
            //结束
            mcrypt_module_close($td);
            $data = $this->removeBR(base64_encode($data));
            return $data;
        }
    
        /**
         * 3des解密
         * 
         * @param    string        $encrypted            密文
         * @return string
         */
        public function decrypt($encrypted)
        {
            $encrypted = base64_decode($encrypted);
            $key = base64_decode($this->key);
            $td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
            //使用MCRYPT_3DES算法,cbc模式
            mcrypt_generic_init($td, $key, $this->iv);
            //初始处理
            $decrypted = mdecrypt_generic($td, $encrypted);
            //解密
            mcrypt_generic_deinit($td);
            //结束
            mcrypt_module_close($td);
            $decrypted = $this->removePadding($decrypted);
            return $decrypted;
        }
    
        /**
         * 填充密码,填充至8的倍数
         * 
         * @param    string        $str            原始字串
         * @return string
         */
        public function padding($str) {
            $len = 8 - strlen($str) % 8;
            for ($i = 0; $i < $len; $i++) {
                $str .= chr(0);
            }
            return $str ;
        }
    
        /**
         * 删除填充符
         * 
         * @param    string        $str            原始字串
         * @return string
         */
        public function removePadding( $str )
        {
            $len = strlen( $str );
            $newstr = "";
            $str = str_split($str);
            for ($i = 0; $i < $len; $i++ )
            {
                if ($str[$i] != chr( 0 ))
                {
                    $newstr .= $str[$i];
                }
            }
            return $newstr;
        }
        
        
        /**
         * 删除回车和换行
         * 
         * @param    string        $str            原始字串
         * @return string
         */
        public function removeBR($str) {
            $len = strlen( $str );
            $newstr = "";
            $str = str_split($str);
            for ($i = 0; $i < $len; $i++) {
                if ($str[$i] != '
    ' && $str[$i] != '
    ') {
                    $newstr .= $str[$i];
                }
            }
            return $newstr;
        }
    }
    ?>
  • 相关阅读:
    Java多态
    推荐TED演讲:20岁光阴不再来(Why 30 is not the new 20)
    HDU 5305 Friends (DFS)
    C#高级编程八十一天----捕获异常
    Amazon EC2安装mysql多实例并配置主从复制
    python coding style guide 的高速落地实践
    Tomcat 在win7/win8 系统下tomcat-users.xml.new(拒绝访问)解决方法
    jsp+oracle实现数据库内容以表格形式在前台显示(包含分页)
    JSP/SERVLET入门教程--Servlet 使用入门
    解决系统打开CHM文件无法正常显示
  • 原文地址:https://www.cnblogs.com/houweijian/p/3342421.html
Copyright © 2011-2022 走看看