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.