【问题标题】:Telegram TCP obfuscated transport not receiving data电报 TCP 混淆传输未接收数据
【发布时间】:2020-08-02 08:05:49
【问题描述】:

我正在尝试实现here 中描述的 MTProto 传输混淆功能。
我已经按照每封信的说明信,但无济于事:发送数据,例如对于ReqPqMulti,但在阅读回复后,例如ResPQ,输入流永远阻塞,表示没有接收到数据。

初始化负载和 AES 密码创建如下。另见 cmets。

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 传输(非混淆)可以正常工作。

【问题讨论】:

    标签: java kotlin aes mtproto


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      相关资源
      最近更新 更多