Encryption and decryption methods implemented by php combined with md5

Source: Internet
Author: User
This article mainly introduces the encryption and decryption methods implemented by php combined with md5, involving PHP string operations and encryption and decryption algorithm implementation skills, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:

Recently, I have found a good thing in the code, combined with the md5 encryption and decryption algorithm. There are few decryption algorithms for php and md5 encryption on the internet. In fact, there is one in the php Manual, just change it. Paste it here and use this algorithm to load a php module mcrypt, otherwise it will not work.

// Encryption function string2secret ($ str) {$ key = "123"; $ td = mcrypt_module_open (MCRYPT_DES, '', 'ECB ',''); $ iv = mcrypt_create_iv (Encrypt ($ td), MCRYPT_RAND); $ ks = mcrypt_enc_get_key_size ($ td); $ key = substr (md5 ($ key), 0, $ ks ); mcrypt_generic_init ($ td, $ key, $ iv); $ secret = mcrypt_generic ($ td, $ str); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td ); return $ secret;} // decrypt function secret2string ($ sec) {$ key = "123"; $ td = mcrypt_module_open (MCRYPT_DES, '', 'ECB ',''); $ iv = mcrypt_create_iv (Encrypt ($ td), MCRYPT_RAND); $ ks = mcrypt_enc_get_key_size ($ td); $ key = substr (md5 ($ key), 0, $ ks ); mcrypt_generic_init ($ td, $ key, $ iv); $ string = mdecrypt_generic ($ td, $ sec); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td ); return trim ($ string);} echo secret2string (string2secret ("11111111111111111"); // The result is 11111111111111111 echo string2secret ("11111111111111111"); // garbled characters are displayed.

Php often uses encryption and decryption functions, such as base64_encode and base64_decode.

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.