Home > Article > Backend Development > PHP des加密输入如何才能和JAVA的des输出一至呢
PHP des加密输入怎么才能和JAVA的des输出一至呢
Java加密代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> /** * 传输的关键数据加密 * @param keyData 需加密值 * @param timestamp 时间戳 * @param dataSecret 密钥 * @return String 加密数据 */ public static String encryptKeyData(String keyData, Long timestamp, String dataSecret) { DateFormat df = new SimpleDateFormat(Constants.DATE_TIME_FORMAT); df.setTimeZone(TimeZone.getTimeZone(Constants.DATE_TIMEZONE)); keyData = df.format(new Date(timestamp)) + "aop" + keyData; return desEncryptToBase64(keyData, dataSecret.getBytes()); }
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> function encrypt($input) { $size = mcrypt_get_block_size('des', 'ecb'); $input = $this->pkcs5_pad($input, $size); $key = $this->key; $td = mcrypt_module_open('des', '', 'ecb', ''); $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); @mcrypt_generic_init($td, $key, $iv); $data = mcrypt_generic($td, $input); mcrypt_generic_deinit($td); mcrypt_module_close($td); $data = base64_encode($data); return $data; } function pkcs5_pad ($text, $blocksize) { $pad = $blocksize - (strlen($text) % $blocksize); return $text . str_repeat(chr($pad), $pad); }