PHPDES encryption and decryption this is a piece of PHP code for DES decryption.
Refer to routines in the self-http://php.net/manual/zh/function.mcrypt-module-open.php. There is no difficulty.
However, after I decrypt the data, I try again. The following are invisible garbled characters.
? ?] Y )? Aw #? Y ???] M? /M? 2 ???] C ?? F ?? V (? I ?????~????? ? E = I "C ??????
It took most of the time to find out that it was because after the other party encrypted the binary ciphertext and sent it to me in hexadecimal format, for example, 13BD5122F55E706D13FB7D0F349A787D19E9D2334E268A15.
As you can see, this cannot be used as the ciphertext itself. I cannot decrypt this hexadecimal string only because it is directly decrypted. First, you need to pass the hexadecimal string back to the binary format, and then decrypt it with the decryption method to see the plaintext.
The hexadecimal to binary method is hex2bin.
Cryptare is used for encryption and decryption. The first parameter is plaintext or ciphertext, the second parameter is key, the third parameter is 0, and the third parameter is encryption.
Note: $ text = $ this-> hex2bin ($ text); may not be used during encryption.
function hex2bin($hexData) { $binData = ""; for($i = 0; $i < strlen ( $hexData ); $i += 2) { $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) ); } return $binData; } function cryptare($text, $key, $crypt) { $text = $this->hex2bin($text); $encrypted_data=""; $td = mcrypt_module_open('des', '', 'ecb', ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM); mcrypt_generic_init($td, $key, $iv); if($crypt) { $encrypted_data = mcrypt_generic($td, $text); print $encrypted_data ; } else { $encrypted_data = mdecrypt_generic($td, $text); } mcrypt_generic_deinit($td); mcrypt_module_close($td); return $encrypted_data; }