php-簡單對稱式加密演算法和字串與十六進位之間的互轉函數

來源:互聯網
上載者:User

標籤:

 1 <?php 2 /** 3 * 簡單對稱式加密演算法之加密 4 * @param String $string 需要加密的字串 5 * @param String $skey 加密EKY 6 * @return String 7 */ 8 function encode($string = ‘‘, $skey = ‘testphp‘) { 9   $skey = str_split(base64_encode($skey));10   $strArr = str_split(base64_encode($string));11   $strCount = count($strArr);12   foreach ($skey as $key => $value) {13     $key < $strCount && $strArr[$key].=$value;14   }15   return str_replace(‘=‘, ‘O0O0O‘, join(‘‘, $strArr));16 }17 18 /**19 * 簡單對稱式加密演算法之解密20 * @param String $string 需要解密的字串21 * @param String $skey 解密KEY22 * @return String23 */24 function decode($string = ‘‘, $skey = ‘testphp‘) {25   $skey = str_split(base64_encode($skey));26   $strArr = str_split(str_replace(‘O0O0O‘, ‘=‘, $string), 2);27   $strCount = count($strArr);28   foreach ($skey as $key => $value) {29     $key < $strCount && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];30   }31   return base64_decode(join(‘‘, $strArr));32 }33 34 //字串轉換成16進位35 function str2hex($str, $encoded = ‘GBK‘) {36   $hex = ‘‘;37   if ($encoded == ‘GBK‘) {38     $str = mb_convert_encoding($str, ‘GBK‘, ‘UTF-8‘);39   }40   for ($i = 0, $length = mb_strlen($str); $i < $length; $i++) {41     $hex .= dechex(ord($str{$i}));42   }43   return $hex;44 }45 46 //16進位轉換成字串47 function hex2str($hex, $encoded = ‘GBK‘) {48   $str = ‘‘;49   $arr = str_split($hex, 2);50   foreach ($arr as $bit) {51     $str .= chr(hexdec($bit));52   }53   if ($encoded == ‘GBK‘) {54     $str = mb_convert_encoding($str, ‘UTF-8‘, ‘GBK‘);55   }56   return $str;57 }

 

php-簡單對稱式加密演算法和字串與十六進位之間的互轉函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.