How can I output the encrypted input in PHPdes with the des in JAVA?

Source: Internet
Author: User
How can I output PHPdes encrypted input and JAVA des? Java encryption code Javacode/*** transmission key data encryption * @ paramkeyData requires encryption value * @ paramtimestamp timestamp * PHP des how can the encrypted input be output together with the JAVA des?
Java encryption code

Java code
  /*** Key data encryption for transmission * @ param keyData requires an encrypted value * @ param timestamp * @ param dataSecret Key * @ return String encrypted data */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 ());}


PHP encryption code

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


The contents encrypted each time are different. it is a headache that PHP must work with JAVA algorithms.

------ Solution --------------------
There are not a lot of online users. can you see if this is helpful?
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 );

}



?>
------ Solution --------------------
I have encountered a similar encryption and decryption problem before. I don't know whether the two languages have different data processing methods or why, but I did not know why. I used javabridge to solve the problem.
------ Solution --------------------
No matching in php
KeyData = df. format (new Date (timestamp) + "aop" + keyData;
Part

The two algorithms are different.

------ Solution --------------------
It is best to paste the java code completely. Copy the file to run the command. it's phper.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.