PHP encryption and decryption instance analysis, _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP encryption and decryption instance analysis ,. PHP encryption and decryption example analysis, this article describes the PHP encryption and decryption method. For your reference, please refer to: encryption functionstring2secret ($ str) {$ key123; $ td PHP encryption and decryption instance analysis,

This article describes the PHP encryption and decryption methods. 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.

Articles you may be interested in:
  • PHP-based SSL encryption and decryption, verification, and signature methods (simple)
  • PHP encryption and decryption string function Source Code Download
  • PHP-encapsulated string encryption and decryption functions
  • Thinkphp development (message encryption and decryption)
  • PHP encryption and decryption functions
  • PHP implements enhanced version encryption and decryption instances
  • PHP rsa encryption and decryption usage
  • PHP encryption and decryption string summary
  • Analysis of PHP encryption/decryption instances
  • Php interface data encryption, decryption, signature verification
  • Php rsa encryption and decryption usage details
  • Php custom encryption and decryption program instance

Example: PHP encryption and decryption methods. For your reference, please refer to the following details: // encrypted function string2secret ($ str) {$ key = "123"; $ td...

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.