DES is a standard data encryption algorithm. For details about this algorithm, refer to Wikipedia and Baidu Encyclopedia:
Wikipedia Baidu encyclopedia
There is an extension in php that supports the DES encryption algorithm: extension = php_mcrypt.dll
Opening the extension in the configuration file cannot be used in windows.
You need to copy libmcrypt. dll in the PHP folder to the system32 directory of the system. Through phpinfo, you can see that mcrypt indicates that this module can be used properly.
The following is an example of using DES for encryption and decryption in PHP:
Copy codeThe Code is as follows: // $ input-stuff to decrypt
// $ Key-the secret key to use
Function do_mencrypt ($ input, $ key)
{
$ Input = str_replace ("" n "," ", $ input );
$ Input = str_replace ("" t "," ", $ input );
$ Input = str_replace ("" r "," ", $ input );
$ Key = substr (md5 ($ key), 0, 24 );
$ Td = mcrypt_module_open ('tripledes ', '', 'ecb ','');
$ Iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND );
Mcrypt_generic_init ($ td, $ key, $ iv );
$ Encrypted_data = mcrypt_generic ($ td, $ input );
Mcrypt_generic_deinit ($ td );
Mcrypt_module_close ($ td );
Return trim (chop (base64_encode ($ encrypted_data )));
}
// $ Input-stuff to decrypt
// $ Key-the secret key to use
Function do_mdecrypt ($ input, $ key)
{
$ Input = str_replace ("" n "," ", $ input );
$ Input = str_replace ("" t "," ", $ input );
$ Input = str_replace ("" r "," ", $ input );
$ Input = trim (chop (base64_decode ($ input )));
$ Td = mcrypt_module_open ('tripledes ', '', 'ecb ','');
$ Key = substr (md5 ($ key), 0, 24 );
$ Iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($ td), MCRYPT_RAND );
Mcrypt_generic_init ($ td, $ key, $ iv );
$ Decrypted_data = mdecrypt_generic ($ td, $ input );
Mcrypt_generic_deinit ($ td );
Mcrypt_module_close ($ td );
Return trim (chop ($ decrypted_data ));
}