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