PHP uses des to encrypt and decrypt the method to explain _php skills

Source: Internet
Author: User
Tags chop decrypt md5 trim

Des is a standard data encryption algorithm, the detailed introduction of this algorithm can refer to Wiki and Baidu Encyclopedia:

Wiki Encyclopedia Baidu Encyclopedia

PHP has an extension to support DES encryption algorithm, is:extension=php_mcrypt.dll

Opening this extension in a configuration file cannot be used in a Windows environment

You need to copy the Libmcrypt.dll under the PHP folder to the System32 directory of the system, which is phpinfo to see mcrypt that the module can be tested properly.

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

Copy Code code 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));

}

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.