Home  >  Article  >  Backend Development  >  Detailed explanation of how PHP uses DES for encryption and decryption_PHP Tutorial

Detailed explanation of how PHP uses DES for encryption and decryption_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:08:54764browse

DES is a standard data encryption algorithm. For a detailed introduction to this algorithm, please refer to wiki and Baidu Encyclopedia:

wikipedia Baidu Encyclopedia

There is an extension in php that can support the DES encryption algorithm, which is: extension=php_mcrypt.dll

Even if this extension is turned on in the configuration file, it cannot be used in the Windows environment

You need to copy libmcrypt.dll in the PHP folder to the system32 directory of the system. You can check mcrypt through phpinfo to indicate that this module can be tried normally.

The following is an example of using DES encryption and decryption in PHP:

Copy the code The 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 =                                                                                       $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); 🎜>
}




http://www.bkjia.com/PHPjc/327421.html

www.bkjia.com

true

TechArticleDES is a standard data encryption algorithm. For a detailed introduction to this algorithm, please refer to wiki and Baidu Encyclopedia: wiki There is an extension in Encyclopedia Baidu Encyclopedia PHP that can support DES encryption algorithm...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn