zoukankan      html  css  js  c++  java
  • php常用des加密解密对应java

    class des{
    
        private $key;
        private $iv;
    
        public function __construct($key, $iv='23456789')
        {
            $this->key = $key;
            $this->iv = $iv;
        }
    
        function fixUrlSafeEncode($str, $isEncrypt = true)
        {
            $str1 = ['=','/','+'];
            $str2 = ['.','_','-'];
    
            if (!$isEncrypt)
            {
                list($str1,$str2) = [$str2,$str1];
            }
    
            return str_replace($str1, $str2, $str);
        }
    
        function encrypt($input)
        {
            $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
            $input = $this->PaddingPKCS7($input);
            @mcrypt_generic_init($td, $this->key, $this->iv);
            $data = mcrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
            $data = $this->fixUrlSafeEncode(base64_encode($data));
            return $data;
        }
    
        function decrypt($encrypted)
        {
            $encrypted = $this->fixUrlSafeEncode($encrypted,false);
    
            $td  = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');
    
            @mcrypt_generic_init($td, $this ->key, $this->iv);
    
            $encrypted = base64_decode($encrypted);
    
            $ret = trim(mdecrypt_generic($td, $encrypted));
    
            $ret = $this->UnPaddingPKCS7($ret);
    
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
    
            return $ret;
        }
    
        function pkcs5_pad($text, $blocksize)
        {
            $pad = $blocksize - (strlen($text) % $blocksize);
            return $text . str_repeat(chr($pad), $pad);
        }
    
        function pkcs5_unpad($text)
        {
            $pad = ord($text{strlen($text) - 1});
            if ($pad > strlen($text)) {
                return false;
            }
            if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
                return false;
            }
            return substr($text, 0, -1 * $pad);
        }
    
        function PaddingPKCS7($data)
        {
            $block_size   = mcrypt_get_block_size('tripledes', MCRYPT_MODE_ECB);
            $padding_char = $block_size - (strlen($data) % $block_size);
            $data .= str_repeat(chr($padding_char), $padding_char);
            return $data;
        }
    
        function UnPaddingPKCS7($text) {
            $pad = ord($text{strlen($text) - 1});
            if ($pad > strlen($text)) {
                return false;
            }
            if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
                return false;
            }
            return substr($text, 0, -1 * $pad);
        }
    }
    

      

  • 相关阅读:
    java_db2错误码对应值
    oracle_用户与概要文件
    quartz配置时间
    bzoj2395: [Balkan 2011]Timeismoney
    bzoj2725: [Violet 6]故乡的梦
    bzoj4400: tjoi2012 桥
    双连通分量模板
    bzoj3047: Freda的传呼机 && 2125: 最短路
    bzoj3541: Spoj59 Bytelandian Information Agency
    bzoj1023: [SHOI2008]cactus仙人掌图
  • 原文地址:https://www.cnblogs.com/wlyxr/p/9836384.html
Copyright © 2011-2022 走看看