Analysis of PHP encryption/decryption instances

Source: Internet
Author: User
This article mainly introduces the implementation of PHP encryption and decryption, and analyzes the related skills of PHP user-defined functions to implement string encryption and corresponding decryption in the form of examples, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:

// 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 echo'
'; // $ String: string to be encrypted $ isEncrypt = true: encrypted $ isEncrypt = false: decrypted function dencrypt ($ string, $ isEncrypt = true, $ key = "KEY_SPACE") {if (! Isset ($ string {0}) |! Isset ($ key {0}) {return false;} $ dynKey = $ isEncrypt? Hash ('sha1', microtime (true): substr ($ string, 0, 40); $ fixedKey = hash ('sha1', $ key ); $ dynKeyPart1 = substr ($ dynKey, 0, 20); $ dynKeyPart2 = substr ($ dynKey, 20); $ fixedKeyPart1 = substr ($ fixedKey, 0, 20 ); $ fixedKeyPart2 = substr ($ fixedKey, 20); $ key = hash ('sha1', $ dynKeyPart1. $ fixedKeyPart1. $ dynKeyPart2. $ fixedKeyPart2); $ string = $ isEncrypt? $ FixedKeyPart1. $ string. $ dynKeyPart2: (isset ($ string {339 })? Gzuncompress (base64_decode (substr ($ string, 40): base64_decode (substr ($ string, 40); $ n = 0; $ result = ''; $ len = strlen ($ string); for ($ n = 0; $ n <$ len; $ n ++) {$ result. = chr (ord ($ string {$ n}) ^ ord ($ key {$ n % 40});} return $ isEncrypt? $ DynKey. str_replace ('=', '', base64_encode ($ n> 299? Gzcompress ($ result): substr ($ result, 20,-20);} echo strlen (dencrypt ("12345678912345 "));

I hope this article will help you with PHP programming.

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.