Home > Backend Development > PHP Tutorial > PHP des加密输入如何才能和JAVA的des输出一至呢

PHP des加密输入如何才能和JAVA的des输出一至呢

WBOY
Release: 2016-06-13 13:36:54
Original
813 people have browsed it

PHP des加密输入怎么才能和JAVA的des输出一至呢
Java加密代码

Java code
<!--

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

Copy after login


PHP加密代码

PHP code
<!--

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

Copy after login


每次加密出来的内容都是不一样的,头疼,必须是PHP配合JAVA的算法

------解决方案--------------------
网上不是蛮多这方面的你看看是否有帮助?
http://www.cnblogs.com/ghj1976/archive/2011/05/26/2058841.html
http://blog.csdn.net/raining_peidx/article/details/4490741


  

  

function encrypt($str, $key)

{

$block = mcrypt_get_block_size('des', 'ecb');

$pad = $block - (strlen($str) % $block);

$str .= str_repeat(chr($pad), $pad);

  

return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);

}

  

function decrypt($str, $key)

{

$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);

  

$block = mcrypt_get_block_size('des', 'ecb');

$pad = ord($str[($len = strlen($str)) - 1]);

return substr($str, 0, strlen($str) - $pad);

}

  

?>
------解决方案--------------------
以前也遇到过类似的加解密问题,不知道是两种语言对数据的处理方式不一样还是其它什么原因,后来实在没辙了,用javabridge解决的
------解决方案--------------------
没有看到 php 中对应
 keyData = df.format(new Date(timestamp)) + "aop" + keyData;
的部分

感觉两者的算法不一致

------解决方案--------------------
最好把java代码贴全咯。copy过来就能运行那种,咱是phper
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template