Skip to content

Commit

Permalink
Revert "Give a fake password for those who decode base64 directly (#4)…
Browse files Browse the repository at this point in the history
…" (#5)

This reverts commit 67ee948.
  • Loading branch information
Ylarod authored Aug 4, 2023
1 parent 67ee948 commit 456079b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 173 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ jobs:
cache: gradle
- name: Build
run: |
cd $GITHUB_WORKSPACE/src/main/kotlin
if [ ! -z "${{ secrets.XXTEA_KEY }}" ]; then
sed -i "s/20221209/"${{ secrets.XXTEA_KEY }}"/" ./Main.kt
fi
cd $GITHUB_WORKSPACE
chmod +x ./gradlew
./gradlew jar
- name: Upload build artifact
Expand Down
170 changes: 2 additions & 168 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,170 +22,6 @@ data class JoinRequest(
val identifier: ChatIdentifier,
val password: String,
)

/**
* XXTEA.kt comes from https://github.com/xJoeWoo/xxtea-kotlin
*/

object XXTEA {

private const val DELTA = -0x61c88647

@Suppress("NOTHING_TO_INLINE", "FunctionName")
private inline fun MX(sum: Int, y: Int, z: Int, p: Int, e: Int, k: IntArray): Int {
return (z.ushr(5) xor (y shl 2)) + (y.ushr(3) xor (z shl 4)) xor (sum xor y) + (k[p and 3 xor e] xor z)
}

fun encrypt(data: ByteArray, key: ByteArray): ByteArray =
data.takeIf { it.count() != 0 }
?.let {
encrypt(data.toIntArray(true), key.fixKey().toIntArray(false))
.toByteArray(false)
}
?: data

fun encrypt(data: String, key: ByteArray): ByteArray? =
runCatching { encrypt(data.encodeToByteArray(throwOnInvalidSequence = true), key) }.getOrNull()

fun encrypt(data: ByteArray, key: String): ByteArray? =
runCatching { encrypt(data, key.encodeToByteArray(throwOnInvalidSequence = true)) }.getOrNull()

fun encrypt(data: String, key: String): ByteArray? =
runCatching {
encrypt(
data.encodeToByteArray(throwOnInvalidSequence = true),
key.encodeToByteArray(throwOnInvalidSequence = true)
)
}.getOrNull()

fun decrypt(data: ByteArray, key: ByteArray): ByteArray =
data.takeIf { it.count() != 0 }
?.let {
decrypt(data.toIntArray(false), key.fixKey().toIntArray(false))
.toByteArray(true)
} ?: data

fun decrypt(data: ByteArray, key: String): ByteArray? =
kotlin.runCatching { decrypt(data, key.encodeToByteArray(throwOnInvalidSequence = true)) }.getOrNull()

fun decryptToString(data: ByteArray, key: ByteArray): String? =
kotlin.runCatching { decrypt(data, key).decodeToString(throwOnInvalidSequence = true) }.getOrNull()

fun decryptToString(data: ByteArray, key: String): String? =
kotlin.runCatching { decrypt(data, key)?.decodeToString(throwOnInvalidSequence = true) }.getOrNull()

private fun encrypt(v: IntArray, k: IntArray): IntArray {
val n = v.size - 1

if (n < 1) {
return v
}
var p: Int
var q = 6 + 52 / (n + 1)
var z = v[n]
var y: Int
var sum = 0
var e: Int

while (q-- > 0) {
sum += DELTA
e = sum.ushr(2) and 3
p = 0
while (p < n) {
y = v[p + 1]
v[p] += MX(sum, y, z, p, e, k)
z = v[p]
p++
}
y = v[0]
v[n] += MX(sum, y, z, p, e, k)
z = v[n]
}
return v
}

private fun decrypt(v: IntArray, k: IntArray): IntArray {
val n = v.size - 1

if (n < 1) {
return v
}
var p: Int
val q = 6 + 52 / (n + 1)
var z: Int
var y = v[0]
var sum = q * DELTA
var e: Int

while (sum != 0) {
e = sum.ushr(2) and 3
p = n
while (p > 0) {
z = v[p - 1]
v[p] -= MX(sum, y, z, p, e, k)
y = v[p]
p--
}
z = v[n]
v[0] -= MX(sum, y, z, p, e, k)
y = v[0]
sum -= DELTA
}
return v
}

private fun ByteArray.fixKey(): ByteArray {
if (size == 16) return this
val fixedKey = ByteArray(16)

if (size < 16) {
copyInto(fixedKey)
} else {
copyInto(fixedKey, endIndex = 16)
}
return fixedKey
}

private fun ByteArray.toIntArray(includeLength: Boolean): IntArray {
var n = if (size and 3 == 0)
size.ushr(2)
else
size.ushr(2) + 1
val result: IntArray

if (includeLength) {
result = IntArray(n + 1)
result[n] = size
} else {
result = IntArray(n)
}
n = size
for (i in 0 until n) {
result[i.ushr(2)] = result[i.ushr(2)] or (0x000000ff and this[i].toInt() shl (i and 3 shl 3))
}
return result
}

private fun IntArray.toByteArray(includeLength: Boolean): ByteArray? {
var n = size shl 2

if (includeLength) {
val m = this[size - 1]
n -= 4
if (m < n - 3 || m > n) {
return null
}
n = m
}
val result = ByteArray(n)

for (i in 0 until n) {
result[i] = this[i.ushr(2)].ushr(i and 3 shl 3).toByte()
}
return result
}
}

suspend fun main(vararg args: String) {
val map = HashMap<ChatIdentifier, JoinRequest>()

Expand All @@ -200,9 +36,7 @@ suspend fun main(vararg args: String) {
onChatJoinRequest {
val model = getModel(it.from?.asCommonUser()?.ietfLanguageCode?.code)
val password = abs(it.chat.id.chatId).toString()
val secret = "20221209"
val fakepassword = XXTEA.encrypt(password.toString(), secret)
val encodedPassword: String = Base64.getEncoder().encodeToString(fakepassword)
val encodedPassword: String = Base64.getEncoder().encodeToString(password.toByteArray())
bot.sendMessage(it.from.id, model.problem.replace("[PASSWORD]", encodedPassword))
map[it.from.id] = JoinRequest(it.chat.id, password)
println("user ${it.from.id} start joining ${it.chat.id}")
Expand Down Expand Up @@ -232,4 +66,4 @@ suspend fun main(vararg args: String) {
}
}
}.second.join()
}
}

0 comments on commit 456079b

Please sign in to comment.