创建电报 auth_key

Create telegram auth_key

我最近开始使用电报 api。第一阶段,我提出了接收auth_key的请求。 这是我的 C# 代码:

// auth_key_id in unencrypted message is ZERO
Int64 auth_key_id = 0;
// this is current time stamp that used as message id
Int64 message_id = DateTime.Now.Ticks;
// message type for req_pq is 0x60469778 in big-ending format
byte[] message_type = {120, 151, 70, 96};
// this is data lenght, it determind in run time
Int32 data_lenght;
// data is combined message_type and an int128 bit value called nonce
// nonce create by random
byte[] nonce = new byte[16];
Random rand = new Random(1);
rand.NextBytes(nonce);
// make data
List<byte> dataList = new List<byte>();
dataList.AddRange(message_type);
dataList.AddRange(nonce);
byte[] data = dataList.ToArray();
// make packet
List<byte> packetList = new List<byte>();
packetList.AddRange(BitConverter.GetBytes(auth_key_id));
packetList.AddRange(BitConverter.GetBytes(message_id));
data_lenght = data.Length;
packetList.AddRange(BitConverter.GetBytes(data_lenght));
packetList.AddRange(data);
byte[] packet = packetList.ToArray();
try
{
    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    s.Connect("149.154.167.40", 443);
    if (s.Connected) 
    {
        IPEndPoint remote = s.RemoteEndPoint as IPEndPoint;
        Console.WriteLine("Connected To : "+remote.Address+":"+remote.Port);
    }
    int sendLength = s.Send(packet);
    Console.WriteLine("Send " +sendLength+" Byte(s)");
    byte[] received = new byte[128];
    int recLen = s.Receive(received);
    Console.WriteLine("Received " + recLen + " Byte(s)");
    Console.ReadKey();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
}

我通过 wireshark 捕获发送数据并获得此有效负载:

0000 10 fe ed f4 8e 97 20 6a 8a 54 28 95 08 00 45 00
0010 00 50 02 a3 40 00 80 06 00 00 c0 a8 01 64 95 9a
0020 a7 28 23 e3 01 bb 0e 4d aa 3b 61 c3 01 b6 50 18
0030 01 01 ff 11 00 00 00 00 00 00 00 00 00 00 af 20
0040 82 e0 b4 08 d3 08 14 00 00 00 78 97 46 60 46 d0
0050 86 82 40 97 e4 a3 95 cf ff 46 69 9c 73 c4

粗体部分是我的有效载荷,根据电报文档,它似乎是正确的,但我没有从服务器收到任何响应。

小提示:我对Telegram一无所知API.

首先,您要连接到 149.154.167.40:443。这是 HTTPS 端点的默认端口,这意味着您的客户端需要 'speak' SSL 才能进行通信。

基本上发生的事情是您连接到端口,服务器等待有效的 SSL 握手。但您实际发送的是实际内容本身。服务器注意到您正在发送某些内容,但由于接收到的字节数少于有效的 SSL 握手,它会一直等待,直到您发送更多内容。

您想要做的是 'speak' 这个 SSL 协议。 SSL 使您发送 and/or 接收的内容被加密。例如,这可以通过使用 SslStream 来完成。这是稍高的级别,但它会阻止您改为实施 SSL。

另一方面,如果可用,您可以尝试连接到端口 80,但这会禁用加密。我不建议这样做。

你的 data.Length 是 40 字节
你忘了其他 12 字节 :)
您必须在发送前封装您的数据

-- 04 字节 -- 数据长度 (data.Length + 12) == 52
-- 04 字节 -- 序列号(从0开始)== 0
-- 40 字节 -- 你的数据
-- 04 字节 -- CRC32(计算 48 字节的哈希值)

现在你的数据包长度是 52 字节
现在你可以发送了
发送后您将收到响应(96 字节)
现在您可以转到第2步进行授权

确定,我几天前做过
祝你好运 !!!