创建电报AUTH_KEY [英] Create telegram auth_key

查看:381
本文介绍了创建电报AUTH_KEY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用电报API工作。在第一阶段,我做了接收 AUTH_KEY 的请求。
这是我的C#代码:

I've recently started to work with the telegram api. In the first stage, I made a request to receive auth_key. This is my c# code :

// 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的发送数据,并得到这个有效载荷:

I capture send data by wireshark and get this payload :

0000 10 FE编F4 8E 97 20 6A 8A 54 28 95 08 00 45 00结果
0010 00 50 02 40 A3 80 00 06 00 00 C0 A8 01 64 95 9A结果
0020 A7 28 23 01 E3 0E BB AA 4D 3B 61 C3 01 B6 50 18结果
0030 01 01 11 FF 00 00的 00 00 00 00 00 00 00 00 20 AF 结果
0040 82 B4 E0 08 08 D3 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

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

中的加粗部分为根据电报单证我的有效载荷,这似乎是正确的,但我不从服务器获取任何回应。

The bold part is my payload and according to telegram documentations it seems correct but i don't get any response from server.

推荐答案

轻微备注:我一无所知电报API

首先,你要连接到 149.154.167.40:443 。这是一个HTTPS端点的默认端口,这意味着你的客户需要以沟通为'说话'SSL。

First off, you're connecting to 149.154.167.40:443. This is the default port of an HTTPS endpoint, which means your client needs to 'speak' SSL in order to communicate.

基本上会发生什么是你连接的端口和服务器等待一个有效的SSL握手。但是你实际上是发送实际的内容本身。服务器通知您要发送的东西,但由于接收的字节数量小于有效的SSL握手,它一直在等待,直到你发送更多。

Basically what happens is that you connect to the port, and the server waits for a valid SSL handshake. But what you're actually sending is the actual content itself. The server notices you are sending something, but since the amount of bytes received is less than a valid SSL handshake, it keeps on waiting until you send more.

您想要什么做相反,是说这个SSL协议。启用S​​SL您发送和/或接收的内容进行加密。这可以通过使用的 SslStream ,例如。这是略高的水平,但它阻止你实现一个SSL实现来代替。

What you want to do instead, is 'speak' this SSL protocol. SSL enabled your sent and/or received content to be encrypted. This can be done by using the SslStream, for example. This is slightly higher level, but it prevents you from implementing an SSL implementation instead.

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

On the other hand, you can try to connect to port 80, if available, but this disables the encryption. I wouldn't recommend doing so.

这篇关于创建电报AUTH_KEY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆