• 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);
        }
    }
    

      

  • 相关阅读:
    react native mapbox MarkView只显示一个子组件问题
    react native mapbox 截图压缩(@react-native-mapbox-glmaps)
    @react-native-mapbox-gl/maps语言插件汉化不完善问题
    SQLSERVER优化
    springboot+react整合
    sqlserver求小数取位
    C#中Math.Round() 的真实含义
    Java Nio学习总结(一)
    Linq去重(自定义字段)
    WPF学习记录(一):布局
  • 原文地址:https://www.cnblogs.com/wlyxr/p/9836384.html
Copyright © 2020-2023  润新知