php-simple symmetric encryption algorithm and reciprocal transfer function between string and hexadecimal

Source: Internet
Author: User
Tags decrypt

1<?PHP2 /**3 * Encryption of simple symmetric encryption algorithm4 * @param string $string strings that need to be encrypted5 * @param String $skey encryption Eky6 * @return String7 */8 functionEncode$string= ",$skey= ' testphp ') {9     $skey=Str_split(Base64_encode($skey));Ten     $STRARR=Str_split(Base64_encode($string)); One     $strCount=Count($STRARR); A     foreach($skey  as $key=$value) { -       $key<$strCount&&$STRARR[$key].=$value; - } the     return Str_replace(' = ', ' o0o0o ',Join(‘‘,$STRARR)); - } -  - /** + * Decryption of simple symmetric encryption algorithm - * @param string $string A string to decrypt + * @param String $skey Decrypt key A * @return String at */ - functionDecode$string= ",$skey= ' testphp ') { -     $skey=Str_split(Base64_encode($skey)); -     $STRARR=Str_split(Str_replace(' o0o0o ', ' = ',$string), 2); -     $strCount=Count($STRARR); -     foreach($skey  as $key=$value) { in       $key<$strCount&&$STRARR[$key][1] = = =$value&&$STRARR[$key] =$STRARR[$key][0]; - } to     return Base64_decode(Join(‘‘,$STRARR)); + } -  the //string converted to 16 binary * functionStr2hex ($str,$encoded= ' GBK ') { $     $hex= ' ';Panax Notoginseng     if($encoded= = ' GBK ') { -       $str= Mb_convert_encoding ($str, ' GBK ', ' UTF-8 '); the } +      for($i= 0,$length= Mb_strlen ($str);$i<$length;$i++) { A       $hex.=Dechex(Ord($str{$i})); the } +     return $hex; - } $  $ //16 binary converted to string - functionHEX2STR ($hex,$encoded= ' GBK ') { -     $str= ' '; the     $arr=Str_split($hex, 2); -     foreach($arr  as $bit) {Wuyi       $str.=CHR(Hexdec($bit)); the } -     if($encoded= = ' GBK ') { Wu       $str= Mb_convert_encoding ($str, ' UTF-8 ', ' GBK '); - } About     return $str; $}

php-simple symmetric encryption algorithm and reciprocal transfer function between string and hexadecimal

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.