PHP uses DES for encryption and decryption

Source: Internet
Author: User
Tags chop
This article provides a detailed analysis of how to use DES for encryption and decryption in PHP. For more information, see

This article provides a detailed analysis of how to use DES for encryption and decryption in PHP. For more information, see

DES is a standard data encryption algorithm. For details about this algorithm, refer to Wikipedia and Baidu Encyclopedia:

Wiki encyclopedia Baidu encyclopedia

There is an extension in php that supports the DES encryption algorithm. For a Hong Kong Vm, It is 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 and the Hong Kong virtual host. This is 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:

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 = 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 ));

}

, Hong Kong server rent

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.