【发布时间】: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 传输(非混淆)可以正常工作。
【问题讨论】: