首页 > 解决方案 > 电报 TCP 混淆传输未接收数据

问题描述

我正在尝试实现此处描述的 MTProto 传输混淆功能。
我已经按照每个字母的说明字母进行操作,但无济于事:发送数据,例如 for ReqPqMulti,但在读取响应时,例如ResPQ,输入流永远阻塞,这意味着没有接收到数据。

初始化有效负载和 AES 密码创建如下。另见评论。

fun buildInitPayload(header: Byte?): Pair<AES, ByteArray> {
   // Add the padded transport header to the reserved words
   val reservedWords = if (header != null) {
      reservedWords + byteArrayOf(header, header, header, header)
   } else {
      reservedWords
   }
   
   val primaryPayload = ByteArray(64)
   val secureRandom = SecureRandom()

   // Generate 64-byte random initialization payload
   do {
      secureRandom.nextBytes(primaryPayload)
   } while (!isInitPayloadValid(reservedWords, primaryPayload))

   if (header != null) {
      primaryPayload[56] = header
      primaryPayload[57] = header
      primaryPayload[58] = header
      primaryPayload[59] = header
   }

   // Extract two keys from both initialization payloads, using bytes at offsets 8-40:
   // the key extracted from the primary payload is used as encryption key
   val encryptionKey = primaryPayload.copyOfRange(8, 40)

   // Generate a secondary initialization payload by reversing the primary payload
   val secondaryPayload = ByteArray(48)
   for (i in 0..47) secondaryPayload[i] = primaryPayload[55 - i]

   // The key extracted from the secondary payload is used as decryption key
   val decryptionKey = secondaryPayload.copyOf(32)

   // Extract two IVs from both initialization payloads, using bytes at offsets 40-56:
   // the IV extracted from the primary payload is used as encryption IV,
   // the IV extracted from the secondary payload is used as decryption IV.
   val encryptionIV = primaryPayload.copyOfRange(40, 56)
   val decryptionIV = secondaryPayload.copyOfRange(32, 48)

   val aes = AES(encryptionKey, encryptionIV, decryptionKey, decryptionIV)
   val encryptedPayload = aes.encryptCTR(primaryPayload)

   return aes to primaryPayload.copyOf(56) + encryptedPayload.copyFrom(56)
}

AES-CTR 密码使用初始化

private val encryptionCipher = Cipher.getInstance("AES/CTR/NoPadding")

init {
   encryptionCipher.init(
      Cipher.ENCRYPT_MODE,
      SecretKeySpec(encryptionKey, "AES"),
      IvParameterSpec(encryptionIV)
   )
}

使用 Abridged 传输(非混淆)可以正常工作。

标签: javakotlinaesmtproto

解决方案


如果使用精简传输有效并且混淆传输根本不响应,则很可能您错过了带有混淆的填充中间传输所需的填充,如下所述:https ://core.telegram.org/mtproto/mtproto-transports#padded -中间的


推荐阅读