Skip to content

Instantly share code, notes, and snippets.

@wilinz
Last active July 2, 2023 07:04
Show Gist options
  • Save wilinz/9ec953ca38038039b55bc4514579c8b6 to your computer and use it in GitHub Desktop.
Save wilinz/9ec953ca38038039b55bc4514579c8b6 to your computer and use it in GitHub Desktop.
谷歌浏览器翻译API破解
const googleTranslateTKK = "448487.932609646";
function shiftLeftOrRightThenSumOrXor(num, optString) {
for (let i = 0; i < optString.length - 2; i += 3) {
let acc = optString.charAt(i + 2);
if ('a' <= acc) {
acc = acc.charCodeAt(0) - 87;
} else {
acc = Number(acc);
}
if (optString.charAt(i + 1) == '+') {
acc = num >>> acc;
} else {
acc = num << acc;
}
if (optString.charAt(i) == '+') {
num += acc & 4294967295;
} else {
num ^= acc;
}
}
return num;
}
function transformQuery(query) {
const bytesArray = [];
let idx = [];
for (let i = 0; i < query.length; i++) {
let charCode = query.charCodeAt(i);
if (128 > charCode) {
bytesArray[idx++] = charCode;
} else {
if (2048 > charCode) {
bytesArray[idx++] = charCode >> 6 | 192;
} else {
if (55296 == (charCode & 64512) && i + 1 < query.length && 56320 == (query.charCodeAt(i + 1) & 64512)) {
charCode = 65536 + ((charCode & 1023) << 10) + (query.charCodeAt(++i) & 1023);
bytesArray[idx++] = charCode >> 18 | 240;
bytesArray[idx++] = charCode >> 12 & 63 | 128;
} else {
bytesArray[idx++] = charCode >> 12 | 224;
}
bytesArray[idx++] = charCode >> 6 & 63 | 128;
}
bytesArray[idx++] = charCode & 63 | 128;
}
}
return bytesArray;
}
function calcHash(query, windowTkk) {
const tkkSplited = windowTkk.split('.');
const tkkIndex = Number(tkkSplited[0]) || 0;
const tkkKey = Number(tkkSplited[1]) || 0;
const bytesArray = transformQuery(query);
let encondingRound = tkkIndex;
for (const item of bytesArray) {
encondingRound += item;
encondingRound = shiftLeftOrRightThenSumOrXor(encondingRound, '+-a^+6');
}
encondingRound = shiftLeftOrRightThenSumOrXor(encondingRound, '+-3^+b+-f');
encondingRound ^= tkkKey;
if (encondingRound <= 0) {
encondingRound = (encondingRound & 2147483647) + 2147483648;
}
const normalizedResult = encondingRound % 1000000;
return normalizedResult.toString() + '.' + (normalizedResult ^ tkkIndex);
}
package com.example
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
const val googleTranslateTKK = "448487.932609646";
const val url = "https://translate.googleapis.com/translate_a/t?anno=3&client=te&v=1.0&format=html&sl=auto&tl="
fun main() {
val queryList = listOf("hello world")
val tk = (calcHash(queryList.joinToString(""), googleTranslateTKK))
println(tk)
val targetLanguage = "zh-CN"
val formBodyBuilder = FormBody.Builder()
queryList.forEach {
formBodyBuilder.add("q", it)
}
val request = Request.Builder()
.url("$url$targetLanguage&tk=$tk")
.header("Content-Type", "application/x-www-form-urlencoded")
.post(formBodyBuilder.build())
.build()
val response = OkHttpClient().newCall(request).execute()
println(response.body?.string())
}
fun calcHash(query: String, windowTkk: String): String {
val tkkSplited = windowTkk.split('.');
val tkkIndex = tkkSplited[0].toInt()
val tkkKey = tkkSplited[1].toLong()
val bytesArray = query.toByteArray().map { it.toUByte() }
var encondingRound = tkkIndex.toLong();
for (item in bytesArray) {
encondingRound += item.toInt()
encondingRound = shiftLeftOrRightThenSumOrXor(encondingRound.toInt(), "+-a^+6");
}
encondingRound = shiftLeftOrRightThenSumOrXor(encondingRound.toInt(), "+-3^+b+-f");
encondingRound = (encondingRound xor tkkKey)
if (encondingRound <= 0) {
encondingRound = ((encondingRound and 2147483647) + 2147483648)
}
val normalizedResult = encondingRound % 1000000;
return normalizedResult.toString() + '.' + (normalizedResult xor tkkIndex.toLong())
}
fun shiftLeftOrRightThenSumOrXor(num: Int, optString: String): Long {
var num1 = num
var i = 0
while (i < optString.length - 2) {
var acc = optString[i + 2].code
if ('a'.code <= acc) {
acc -= 87;
} else {
acc = optString[i + 2].digitToInt()
}
acc = if (optString[i + 1] == '+') {
(num1 ushr acc)
} else {
(num1 shl acc)
}
num1 = if (optString[i] == '+') {
num1 + (acc.toLong() and 4294967295).toInt()
} else {
num1 xor acc
}
i += 3
}
return num1.toUInt().toLong()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment