String encryption and decryption algorithm _php tutorial

Source: Internet
Author: User
Tags mcrypt
There are more reliable and convenient encryption methods in the php5.5. A friend who likes to delve into the study:

Password_hash ()

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

Two string encryption and decryption algorithms based on MCrypt extension, bitwise XOR or summary

 Php/** * @info string encryption and decryption algorithm one, using mcrypt extension * @param string $string pending String * $action ENCODE, encryption | DECODE, Decrypt * @return string $RETURNSTR * @date 2014/4/22 * @author tonglei*/functionMcrypt_handle_string ($string,$action= ' ENCODE '){        !Is_array($string) orExit; $action= = ' DECODE ' &&$string=Base64_decode($string); $key= "123456";//key can be customized or obtained in the configuration file        $mcryptAlgorithm= Mcrypt_des;//Choosing a Cryptographic algorithm        $mcryptMode= MCRYPT_MODE_ECB;//Select an encryption mode        $mcryptIv= Mcrypt_create_iv (Mcrypt_get_iv_size ($mcryptAlgorithm,$mcryptMode),Mcrypt_rand); //creating an initialization vector        $returnstr=Base64_encode(Mcrypt_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 using bitwise XOR or * @param string $string pending String * @param $action ENCODE Encryption | DECODE decryption * @return string*/functionStrcode ($string,$action= ' ENCODE '){        $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);}

http://www.bkjia.com/PHPjc/769761.html www.bkjia.com true http://www.bkjia.com/PHPjc/769761.html techarticle There are more reliable and convenient encryption methods in the php5.5. Like to delve into the friend can understand: Password_hash () http://www.php.net/manual/zh/function.password-hash.php based on mcrypt extension ...

  • 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.