要跟java 作接口交互
对方传过来参数是用 des加密的, php 没有直接des的函数, 网上找了几个好像也不怎么好用,请教高手,有知道的马?

解决方案 »

  1.   

    http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html
      

  2.   

    1楼 提供的好像不行啊
    2楼高手 是否可讲清楚点我是想解密 java里加密过来的des的密文, 唉,  郁闷啊
      

  3.   


    function idtag_des_decode($key,$encrypted)
    {
        $encrypted = base64_decode($encrypted);    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        $ks = mcrypt_enc_get_key_size($td);
        mcrypt_generic_init($td, $key, $key);       //初始处理    $decrypted = mdecrypt_generic($td, $encrypted);       //解密    mcrypt_generic_deinit($td);       //结束
        mcrypt_module_close($td);    $y=pkcs5_unpad($decrypted);
        return $y;
    }function idtag_des_encode($key,$text)
    {    $y=pkcs5_pad($text);    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
       /// $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        $ks = mcrypt_enc_get_key_size($td);
        mcrypt_generic_init($td, $key, $key);       //初始处理
        $encrypted = mcrypt_generic($td, $y);       //解密
        mcrypt_generic_deinit($td);       //结束
        mcrypt_module_close($td);    return base64_encode($encrypted);
    }function pkcs5_pad($text,$block=8)
    {
            $pad = $block - (strlen($text) % $block);
            return $text . str_repeat(chr($pad), $pad);
    }
    function pkcs5_unpad($text)
    {
       $pad = ord($text{strlen($text)-1});
       if ($pad > strlen($text)) return $text;
       if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
       return substr($text, 0, -1 * $pad);
    }$key = 'ABCD1234';
    $str = 'JAVA DES 123';
    $encstr = idtag_des_encode($key,$str);//$tmp = "BJ1g7SNWtIAcmlrdN+Ftaw==";
    $ys=idtag_des_decode($key,$encstr);
    echo $ys;
      

  4.   

    楼上朋友,你好, 谢谢你贴的代码出来, 我测了下, 在我这里php 加密解密都正常。但是我解一个java的密文, 始终解不出来?  有时候会出现乱码, 是不是java 和php des算法中位数 或者进制不同? 
      

  5.   

    什么方式?
    cbc ecb?
      

  6.   

    是用4楼朋友的代码 ,应该是  cbc 模式的吧网页编码没有问题  我用utf8
      

  7.   

    MCRYPT_MODE_ECB (electronic codebook) is suitable for random data, such as encrypting other keys. Since data there is short and random, the disadvantages of ECB have a favorable negative effect. 
    MCRYPT_MODE_CBC (cipher block chaining) is especially suitable for encrypting files where the security is increased over ECB significantly. 
    MCRYPT_MODE_CFB (cipher feedback) is the best mode for encrypting byte streams where single bytes must be encrypted. 
    MCRYPT_MODE_OFB (output feedback, in 8bit) is comparable to CFB, but can be used in applications where error propagation cannot be tolerated. It's insecure (because it operates in 8bit mode) so it is not recommended to use it. 
    MCRYPT_MODE_NOFB (output feedback, in nbit) is comparable to OFB, but more secure because it operates on the block size of the algorithm. 
    MCRYPT_MODE_STREAM is an extra mode to include some stream algorithms like WAKE or RC4. 
      

  8.   

    参考4楼朋友的代码:
    结果如下:$input="JAVADES123";
    $key = "12345678";
    PHP des 密文:eBNDnJT8cWfd922muAxfLw==
    java des密文:  lAyrJ7vmnKcjrdKga+BilA==不知为什么 ?
    而且直接那  java 的密文" lAyrJ7vmnKcjrdKga+BilA== " 来解密 ,解出来也是乱码