代码拿去
红包拿来
- <?phpfunction EncrypKey($Src, $Key='') { $KeyLen = strlen($Key); if ($KeyLen == 0) $Key = 'Think Space'; $KeyPos = 0; try { $Offset = random_int(0, 255); } catch (Exception $e) { $Offset = rand(0, 255); } $Dest = sprintf('%02X', $Offset); for ($SrcPos = 0; $SrcPos < strlen($Src); $SrcPos++) { $SrcAsc = (ord($Src[$SrcPos]) + $Offset) % 255; if ($KeyPos < $KeyLen) $KeyPos += 1; else $KeyPos = 0; $SrcAsc ^= ord($Key[$KeyPos]); $Dest .= sprintf('%02X', $SrcAsc); $Offset = $SrcAsc; } return $Dest;}function UncrypKey($Src, $Key='') { $KeyLen = strlen($Key); if ($KeyLen == 0) $Key = 'Think Space'; $KeyPos = 0; $SrcPos = 0; $Offset = hexdec(substr($Src, 0, 2)); $SrcPos = 2; $Dest = ""; while ($SrcPos < strlen($Src)) { $SrcAsc = hexdec(substr($Src, $SrcPos, 2)); if ($KeyPos < $KeyLen) $KeyPos += 1; else $KeyPos = 1; $TmpSrcAsc = $SrcAsc ^ ord($Key[$KeyPos-1]); if ($TmpSrcAsc <= $Offset) $TmpSrcAsc += 255 - $Offset; else $TmpSrcAsc -= $Offset; $Dest .= chr($TmpSrcAsc); $Offset = $SrcAsc; $SrcPos += 2; } return $Dest;}echo EncrypKey('测试');echo PHP_EOL;echo UncrypKey('00B23C9328834D');?>
复制代码 |