2017-09-28 235 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

相关问题