<?php
class aazdes {
private static $_instance = NULL;
/**
* @return aazdes
*/
public static function share() {
if (is_null(self::$_instance)) {
self::$_instance = new aazdes();
}
return self::$_instance;
}
/**
* 加密解密
* @param string $strText 要处理的字符串
* @param string $isEncrypt true=加密 false=解密
* @return string
*/
public function mycode($strText, $isEncrypt=true) {
//string aa, bb, cc, dd, kk;
$cc='';
$kk = (8 * 101 * 100000 + 4 * 11 * 1000 + 3 * 7 * 8).'';
if ($isEncrypt) {
$aa = $this->encode($strText, $kk);
$dd='';
for($ii=0;$ii<16;$ii++){
$dd.=$aa[rand(0, 15)];
}
$cc = substr($dd,8, 4) . substr($aa,0, 4) . substr($dd,0, 4) . substr($aa,4, 8) . substr($dd,12, 4) . substr($aa,12) . substr($dd,4, 4);
} else {
$aa = $strText;
$bb = substr($aa,4, 4) . substr($aa,12, 8) . substr($aa,24, strlen($aa) - 24 - 4);
$cc = $this->decode($bb, $kk);
}
return $cc;
}
/**
* 加密
* @param string $str 要处理的字符串
* @param string $key 加密Key,为8个字节长度
* @return string
*/
public function encode($str, $key, $tobase64=false) {
$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);//MCRYPT_MODE_CBC
$str = $this->pkcs5Pad($str, $size);
$aaa = mcrypt_cbc(MCRYPT_DES, $key, $str, MCRYPT_ENCRYPT, $key);
if($tobase64){
$ret = base64_encode($aaa);
}else{//密文以十六进制显示
$ret =strtoupper(bin2hex($aaa));
}
return $ret;
}
/**
* 解密
* @param string $str 要处理的字符串
* @param string $key 解密Key,为8个字节长度
* @return string
*/
public function decode($str, $key, $tobase64=false) {
if($tobase64){
$strBin = base64_decode($str);
}else{//密文以十六进制显示的
$strBin = hex2bin($str);
}
$str = mcrypt_cbc(MCRYPT_DES, $key, $strBin, MCRYPT_DECRYPT, $key);
$str = $this->pkcs5Unpad($str);
return $str;
}
function hex2bin($hexData) {
$binData = "";
$imax=strlen($hexData)/2;
for ($i = 0; $i < $imax; $i += 2) {
$binData .= chr(hexdec(substr($hexData, $i, 2)));
}
return $binData;
}
function pkcs5Pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5Unpad($text) {
$pad = ord($text {strlen($text) - 1});
if ($pad > strlen($text))
return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
return false;
return substr($text, 0, - 1 * $pad);
}
public function getSubString($strfull, $strhead, $strfoot='') {
$pos1=0;
$pos2=0;
$result = "";
$pos1 = strpos($strfull,$strhead);
if ($pos1 !== false) {
if($strfoot==''){
$result = substr($strfull,$pos1 + strlen($strhead));
}else{
$pos2 = strpos($strfull,$strfoot, $pos1 + strlen($strhead));
if ($pos2 !== false) {
$result = substr($strfull,$pos1 + strlen($strhead), $pos2 - $pos1 - strlen($strhead));
}
}
}
return $result;
}
public function gbk2utf($str){
return iconv("GBK", "UTF-8", $str);
}
public function utf2gbk($str){
return iconv("UTF-8", "GBK", $str);
}
}
?>
<?php
date_default_timezone_set("Asia/Shanghai");
//引用、定义
define('ABS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
include(ABS_PATH.'aazdes.class.php');
$des = new aazdes();
echo ' <meta charset="UTF-8">';
//构造要发送的数据
$url='http://domain.url?message=';
$username=$des->mycode("czy001",true);
$pass=$des->mycode("123456",true);
$data=[
"username"=>$username,
"password"=>$pass
];
$message=[
"action"=>"login",
"token"=>"TOKEN",
"session"=>"",
"deviceid"=>"",
"message"=>json_encode($data)
];
echo '<hr>要发送的数据:';
print_r($message);
//加密
$message= $des->mycode(json_encode($message),true);
//发送到服务器并返回结果
$_GET['message']=getCurl( $url . base64_encode($message));
//解密数据
$retmessage=isset($_GET['message'])? base64_decode($_GET['message']) : '';
$retmessage= $des->mycode($retmessage,false);//解密
$retmessage=$des->gbk2utf($retmessage);
$data=json_decode($retmessage,true);//true参数强制转为数组 $data['username'] ,否则变为对象 $data->username
//输出结果
echo '<hr>返回的数据:';
print_r($data);
function getCurl( $url, $second = 30) {
$ch = curl_init();
// 设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验
// 设置header
curl_setopt($ch, CURLOPT_HEADER, false);
// 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// post提交方式
curl_setopt($ch, CURLOPT_POST, false);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
// 运行curl
$data = curl_exec($ch);
curl_close($ch);
return $data;
}