2017-09-28 239 views
0

我有一種情況需要對TCP請求進行反向工程。使用wireshark我將請求(二進制序列化的字節數組)作爲一個字符串複製到VS.我需要反序列化字符串以查看正在傳遞的信息。C#將字節字符串轉換爲字節數組

這是我的字符串看起來像:

0000 d4 81 d7 dd 44 37 00 01 45 03 ec ef 08 00 45 00 ....D7..E.....E. 
0010 00 5f 91 b4 40 00 40 06 c2 a6 0a d4 ee 47 0a d4 [email protected]@......G.. 
0020 e2 4e 04 01 e4 9d 75 98 96 de cb 2a 29 25 50 18 .N....u....*)%P. 
0030 27 10 fc f1 00 00 43 50 43 52 00 01 19 00 00 00 '.....CPCR...... 
0040 37 00 00 00 1e 00 00 00 05 01 05 01 3c 00 00 32 7...........<..2 
0050 00 02 ff ff 01 00 4d 54 20 52 61 63 6b 00 00 00 ......MT Rack... 
0060 01 04 03 02 41 05 01 00 00 00 00 00 00   ....A........ 

我怎麼去反序列化它看到的內容?

+3

你能保存/重定向/傾倒在Wireshark的文件? – Sinatr

+1

當然你可以用一些'Substring's和'Replace(「」,「」)'s來解析每個行的中間部分爲[hex string](https://stackoverflow.com/q/321370/1997232) 。 – Sinatr

+0

你是如何複製字符串的?手動或使用別的東西? –

回答

0

我貼你的字符串轉換成一個文件,然後運行下面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Globalization; 

namespace ConsoleApplication7 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.txt"; 
     static void Main(string[] args) 
     { 
      StreamReader reader = new StreamReader(FILENAME); 

      string input = ""; 
      List<byte> data = new List<byte>(); 
      while((input = reader.ReadLine()) != null) 
      { 
       string byteStr = input.Substring(6, 3 * 16).Trim(); 
       data.AddRange(byteStr.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => byte.Parse(x, NumberStyles.HexNumber))); 
      } 

     } 
    } 

} 
+1

產生的時髦字符發生了什麼? ;) –

+0

我不知道。 WireShark中的數據中只有很少的ASCII碼。 – jdweng

相關問題