The RotXUTF8() function is not working properly after HTTP encryption and decryption.

Just starting out? Need help? Post your questions and find answers here.
goomoo
User
User
Posts: 43
Joined: Sun Dec 05, 2004 9:25 am
Location: China
Contact:

The RotXUTF8() function is not working properly after HTTP encryption and decryption.

Post by goomoo »

The RotXUTF8() function is not working properly after HTTP encryption and decryption. It works normally in Purebasic and PHP encryption and decryption, but after HTTP, it is not working properly. What is the problem?

RotXUTF()函数经过 HTTP 加密解密后不正常,在Purebasic 和 PHP 加密和解密是正常的,经过了HTTP 之后就不正常了,这究竟是什么问题呢?

这是Purebasic代码:
This is Purebasic code:

Code: Select all

;RotXUTF8 HTTP Encoding And decoding issues
;RotXUTF8 HTTP 编码解码问题

; RotXUTF8 加密解密。其中X指的移位数,可以支持UTF8编码。
; @param text.s 输入字符串。
; @param bEncode.l 加密或者解密。如果为0,则为解密,如果此值不为0,则加密。
; @param iShiftNum.l 表示移位的位数。如果为0,则表示原样返回。iShiftNum.l 最大值应该小于32。加密和解密时iShiftNum相同。
; @return.s 转换后的字符串
Procedure.s RotXUTF8(text.s, bEncode = 1, iShiftNum.l = 17)
  If iShiftNum = 0
      ProcedureReturn text
  EndIf

  Protected result.s = "" ; 存储最终结果
  Protected char.s ; 临时存储单个字符
  Protected ascii.i ; 存储字符的 ASCII 码

  iLenth.l = Len(text)
  ; 遍历输入字符串的每个字符
  For i = 1 To iLenth
      char = Mid(text, i, 1) ; 取出第i个字符(PureBasic字符串索引从1开始)
      ascii = Asc(char) ; 获取字符的ASCII码(对应Python的ord())

      If bEncode = 0
          ascii = ascii-iShiftNum
      Else
          ascii = ascii + iShiftNum
      EndIf

      result + Chr(ascii)
  Next i

  ProcedureReturn result ; 返回处理后的字符串

EndProcedure
  
sUrl.s="http://localhost:6923/_examples/test_rotXUTF8.php"
;Key.s="ZeroNullBit"
sText.s="This is the String to Crypt  www.znb.cc ☯飞龙在天。www.znb.cc"  ;
Debug "Original: " + sText
;Original: This is the String to Crypt  www.znb.cc ☯飞龙在天。www.znb.cc

sTextEnc.s=RotXUTF8(sText,1)
Debug "Encoded: " + sTextEnc
sTemp.s=RotXUTF8(sTextEnc,0)
Debug "Decoded: " + sTemp
; Original: This is the String To Crypt  www.znb.cc ☯飞龙在天。www.znb.cc
; Encoded: eyz„1z„1…yv1d…ƒzx1…€1TƒŠ…11ˆˆˆ?‹s?tt1♀飯龪圹夺〓ˆˆˆ?‹s?tt
; Decoded: This is the String To Crypt  www.znb.cc ☯飞龙在天。www.znb.cc
; ↑  The above is correct. 上面是对的。

sSubimit.s="encoded=1&data="+URLEncoder(RotXUTF8(sText,1))
NewMap Header$()
Header$("Content-Type") = "application/x-www-form-urlencoded"
Header$("User-Agent") = "Firefox 54.0"
HttpRequest=HTTPRequest(#PB_HTTP_Post,sUrl,sSubimit,#PB_HTTP_NoRedirect,Header$())


If HttpRequest
  Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
  sRespones.s=HTTPInfo(HTTPRequest, #PB_HTTP_Response)
  Debug "Response: " + sRespones
  ;StatusCode: 200
  ;Response: Thi s i s  the S t ring  t o C r y p t   w w w. znb.cc шoؒ ح ԋ ԓ  o  w w w. znb.cc

  ;lResponeSize.l=StringByteLength(sRespones,#PB_Ascii)
  ;*sRespones=AllocateMemory(MemorySize(lResponeSize))
  ;PokeS(*sRespones,sRespones,lResponeSize,#PB_UTF8)
  *sRespones=UTF8(sRespones)
  Debug "turn to UTF8: " + PeekS(*sRespones,-1, #PB_UTF8)
  ;turn to UTF8: Thi s i s  the S t ring  t o C r y p t   w w w. znb.cc шoؒ ح ԋ ԓ  o  w w w. znb.cc
  ;↑↑↑↑   It's the same. The decoded result is different from the one above. How can I correctly modify Purebasic code?
  ;↑↑↑↑   也是一样。跟上面那处 Decoded 的结果不同。我如何正确的改 Purebasic 的代码?

  Debug "turn to Unicode: " + PeekS(*sRespones,-1, #PB_Unicode)
  ;turn To Unicode: 桔붿⁳붿⁳뿯璽敨匠뿯璽뿯犽湩⁧뿯璽뿯澽䌠뿯犽뿯禽뿯炽뿯璽†뿯瞽뿯瞽뿯瞽붿湺⹢捣턠澈鋘뿯𿞭붿诔뿯풽붿뿯澽뿯붿붿붿⹷뿯窽扮挮੣ഊ
  
  Debug "turn to Ascii: " + PeekS(*sRespones,-1, #PB_Ascii)
  ;turn to Ascii: Thi锟絪 i锟絪 锟絫he S锟絫锟絩ing 锟絫锟給 C锟絩锟統锟絧锟絫  锟絯锟絯锟絯.锟絲nb.cc 褕o貟锟截拷詪锟皆擄拷锟給锟斤拷w锟絯锟絯.锟絲nb.cc
  
  FreeMemory(*sRespones)
  FinishHTTP(HTTPRequest)
EndIf

This is PHP code:

Code: Select all

<?php
header("Content-Type: text/html; charset=utf-8");

/**
 * RotXUTF8 加密解密
 * @param string $text 输入字符串
 * @param int $bEncode 加密或者解密。如果为0,则为解密,如果此值不为0,则加密
 * @param int $iShiftNum 表示移位的位数。如果为0,则表示原样返回。iShiftNum最大值应该小于32。加密和解密时iShiftNum相同
 * @return string 转换后的字符串
 */
function RotXUTF8($text, $bEncode = 1, $iShiftNum = 17) {
    if ($iShiftNum == 0) {
        return $text;
    }

    $result = ''; // 存储最终结果
    $length = strlen($text);
    
    // 遍历输入字符串的每个字符
    for ($i = 0; $i < $length; $i++) {
        $char = $text[$i]; // 取出第i个字符
        $ascii = ord($char); // 获取字符的ASCII码

        if ($bEncode == 0) {
            $ascii = $ascii - $iShiftNum;
        } else {
            $ascii = $ascii + $iShiftNum;
        }

        $result .= chr($ascii);
    }

    return $result; // 返回处理后的字符串
}

// // 测试代码 Test Code
// // 测试5:包含特殊字符的字符串 A string containing special characters
// $test5 = "Special chars: !@#$%^&*() Hello, World! ☯飞龙在天 123 ABCxyz";
// $encrypted4 = RotXUTF8($test5);
// $decrypted4 = RotXUTF8($encrypted4, 0);
// echo "测试5 - 原始字符串: {$test5}\n";
// echo "测试5 - 加密后: {$encrypted4}\n";
// echo "测试5 - 解密后: {$decrypted4}\n";
// echo "测试5 - 加密解密是否成功: " . ($test5 === $decrypted4 ? "是" : "否") . "\n\n";
// // 测试5 - 原始字符串: Special chars: !@#$%^&*() Hello, World! ☯飞龙在天 123 ABCxyz
// // 测试5 - 加密后: d�vtzr}1tyr��K12Q456o7;9:1Yv}}�=1h��}u21������Ϫ������1BCD1RST���
// // 测试5 - 解密后: Special chars: !@#$%^&*() Hello, World! ☯飞龙在天 123 ABCxyz
// // 测试5 - 加密解密是否成功: 是


//POST 测试 
$data=$_POST["data"];
$data=urldecode($data);
$data=RotXUTF8($data,0);
echo "{$data}\n\n";

?>

Hello, Everyone.
Thanks for your help.