String encryption and decryption algorithm

Source: Internet
Author: User
Tags mcrypt

Php5.5 provides more reliable and convenient encryption methods. For more information, see:

Password_hash ()

Http://www.php.net/manual/zh/function.password-hash.php

Two string encryption and decryption algorithms based on the mcrypt Extension

<? Php/*** @ info string encryption and decryption algorithm 1. Use mcrypt extension * @ param string $ string to be processed * $ action ENCODE, encryption | DECODE, decryption * @ return string $ returnstr * @ date 2014/4/22 * @ author tonglei */function mcrypt_handle_string ($ string, $ action = 'encoding '){! Is_array ($ string) or exit; $ action = 'decode' & $ string = base64_decode ($ string); $ key = "123456 "; // key can be customized or obtained in the configuration file $ mcryptAlgorithm = MCRYPT_DES; // select an encryption algorithm $ mcryptMode = MCRYPT_MODE_ECB; // select an encryption mode $ mcryptIv = mcrypt_create_iv (encrypt ($ encrypt, $ mcryptMode), MCRYPT_RAND); // create an initialization vector $ returnstr = base64_encode (encrypt ($ mcryptAlgorithm, $ key, $ string, $ mcryptMode, $ mcryptIv); if ('decode' = $ action) {$ returnstr = mcrypt_decrypt ($ mcryptAlgorithm, $ key, $ string, $ mcryptMode, $ mcryptIv);} return $ returnstr ;}

 

<? Php/*** @ info string encryption and decryption algorithm 2 adopts bitwise XOR or * @ param string $ string to be processed * @ param $ action ENCODE encryption | DECODE decryption * @ return string */function StrCode ($ string, $ action = 'encoding') {$ action! = 'Encode' & $ string = base64_decode ($ string); $ code = ''; $ key = substr (md5 ($ GLOBALS ['pwserver'] ['HTTP _ USER_AGENT ']. $ GLOBALS ['db _ hash']), 8, 18); $ keyLen = strlen ($ key); $ strLen = strlen ($ string ); for ($ I = 0; $ I <$ strLen; $ I ++) {$ k = $ I % $ keyLen; $ code. = $ string [$ I] ^ $ key [$ k];} return ($ action! = 'Decode '? Base64_encode ($ code): $ code );}

 

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.