Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
feat(en/kisskh): Decrypt encrypted subtitles (aniyomiorg#2646)
Browse files Browse the repository at this point in the history
  • Loading branch information
Secozzi authored Dec 14, 2023
1 parent 99d4cc0 commit 06cbdfe
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/en/kisskh/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ext {
extName = 'KissKH'
pkgNameSuffix = 'en.kisskh'
extClass = '.KissKH'
extVersionCode = 2
extVersionCode = 3
libVersion = '13'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.animesource.online.AnimeHttpSource
import eu.kanade.tachiyomi.network.GET
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
Expand All @@ -21,7 +20,6 @@ import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import uy.kohesive.injekt.api.get

class KissKH : AnimeHttpSource() {

Expand Down Expand Up @@ -111,6 +109,8 @@ class KissKH : AnimeHttpSource() {
return videosFromElement(response, id)
}

private val subDecryptor by lazy { SubDecryptor(client, headers, baseUrl) }

private fun videosFromElement(response: Response, id: String): List<Video> {
val jsonData = response.body.string()
val jObject = json.decodeFromString<JsonObject>(jsonData)
Expand All @@ -122,10 +122,16 @@ class KissKH : AnimeHttpSource() {
try {
val suburl = item.jsonObject["src"]!!.jsonPrimitive.content
val lang = item.jsonObject["label"]!!.jsonPrimitive.content
subList.add(Track(suburl, lang))

if (suburl.endsWith("txt")) {
subList.add(subDecryptor.getSubtitles(suburl, lang))
} else {
subList.add(Track(suburl, lang))
}
} catch (_: Error) {}
}
val videoUrl = jObject["Video"]!!.jsonPrimitive.content

videoList.add(Video(videoUrl, "FirstParty", videoUrl, subtitleTracks = subList, headers = Headers.headersOf("referer", "https://kisskh.me/", "origin", "https://kisskh.me")))

return videoList.reversed()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package eu.kanade.tachiyomi.animeextension.en.kisskh

import android.net.Uri
import android.util.Base64
import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.network.GET
import okhttp3.Headers
import okhttp3.OkHttpClient
import java.io.File
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

class SubDecryptor(private val client: OkHttpClient, private val headers: Headers, private val baseurl: String) {
fun getSubtitles(subUrl: String, subLang: String): Track {
val subHeaders = headers.newBuilder().apply {
add("Accept", "application/json, text/plain, */*")
add("Origin", baseurl)
add("Referer", "$baseurl/")
}.build()

val subtitleData = client.newCall(
GET(subUrl, subHeaders),
).execute().use { it.body.string() }

val chunks = subtitleData.split(CHUNK_REGEX)
.filter(String::isNotBlank)
.map(String::trim)

val decrypted = chunks.mapIndexed { index, chunk ->
val parts = chunk.split("\n")
val text = parts.slice(1 until parts.size)
val d = text.map { decrypt(it) }.joinToString("\n")

arrayOf(index + 1, parts.first(), d).joinToString("\n")
}.joinToString("\n\n")

val file = File.createTempFile("subs", "srt")
.also(File::deleteOnExit)

file.writeText(decrypted)
val uri = Uri.fromFile(file)

return Track(uri.toString(), subLang)
}

companion object {
private val CHUNK_REGEX by lazy { Regex("^\\d+$", RegexOption.MULTILINE) }

private val KEY = intArrayOf(942683446, 876098358, 875967282, 943142451)
private val IV = intArrayOf(909653298, 909193779, 925905208, 892483379)
}

private fun getKey(words: IntArray): SecretKeySpec {
val keyBytes = words.toByteArray()
return SecretKeySpec(keyBytes, "AES")
}

private fun decrypt(data: String): String {
val key = getKey(KEY)
val iv = IvParameterSpec(IV.toByteArray())

val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
cipher.init(Cipher.DECRYPT_MODE, key, iv)

val encryptedBytes = Base64.decode(data, Base64.DEFAULT)
return String(cipher.doFinal(encryptedBytes), Charsets.UTF_8)
}

private fun IntArray.toByteArray(): ByteArray {
return ByteArray(size * 4).also { bytes ->
forEachIndexed { index, value ->
bytes[index * 4] = (value shr 24).toByte()
bytes[index * 4 + 1] = (value shr 16).toByte()
bytes[index * 4 + 2] = (value shr 8).toByte()
bytes[index * 4 + 3] = value.toByte()
}
}
}
}

0 comments on commit 06cbdfe

Please sign in to comment.