今天在作項目中,由於要調用別人網站的接口,結果須要對請求和返回的時間進行十六進制加密處理,因而在網上查了下資料謝了一個轉換Demo作個記錄。php
若是在TP下使用能夠將下面函數放到common.php中函數
一,加密函數測試
<?php
/**
*字符串轉十六進制函數
*@pream string $str='abc';
*/
function strToHex($str){
$hex="";
for($i=0;$i<strlen($str);$i++)
$hex.=dechex(ord($str[$i]));
$hex=strtoupper($hex);
return $hex;
}
?>網站
2、解密函數
<?php
/**
*十六進制轉字符串函數
*@pream string $hex='616263';
*/
function hexToStr($hex){
$str="";
for($i=0;$i<strlen($hex)-1;$i+=2)
$str.=chr(hexdec($hex[$i].$hex[$i+1]));
return $str;
}
?>加密
加密 解密 轉換 函數使用Demo事例,這裏爲了方便寫在了一個類中。
<?php
class Test{
/**
*字符串轉十六進制函數
*@pream string $str='abc';
*/
public function strToHex($str){
$hex="";
for($i=0;$i<strlen($str);$i++)
$hex.=dechex(ord($str[$i]));
$hex=strtoupper($hex);
return $hex;
}
/**
*十六進制轉字符串函數
*@pream string $hex='616263';
*/
public function hexToStr($hex){
$str="";
for($i=0;$i<strlen($hex)-1;$i+=2)
$str.=chr(hexdec($hex[$i].$hex[$i+1]));
return $str;
}
}
<span style="white-space:pre"> </span>//測試Demo效果
$test = new Test();
$str = '要加密的內容sxfenglei';
$data = $test->strToHex($str);
echo '加密內容:要加密的內容sxfenglei <br>'.$data.'<hr>';
$output = $test->hexToStr($data);
echo '解密內容:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569 <br>'.$output;
?>spa
運行結果:
加密內容:要加密的內容sxfenglei
E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569
解密內容:E8A681E58AA0E5AF86E79A84E58685E5AEB9737866656E676C6569
要加密的內容sxfenglei.net
————————————————
版權聲明:本文爲CSDN博主「碎碎馮同學」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/fl442165035/article/details/49154891blog