Use DES in PHP for encryption and decryption

Source: Internet
Author: User
Tags chop
PHP uses DES for encryption and decryption. DES is a standard data encryption algorithm. for details about this algorithm, refer to Wikipedia and Baidu: Wikipedia & nbsp; & nbsp; there is an extension in Baidu Encyclopedia php that supports the DES encryption algorithm. it is: extension = php_mcrypt.dll. in the configuration file, this extension is played in PHP and DES is used for encryption and decryption.

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: 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:


// $ 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.