前面介绍了有道翻译接口 破解,接下来试一试谷歌翻译(Github项目地址)
同样,查看首页元素,如下:
得到 Request URL 和 Request Method。
然后查看 From Data,可以看到一系列的参数,不过我们重点只需要关注 tk。
按照推断,tk 参数值也是一种签名,通过某种方法得到。我们从众多的 js 代码中可以发现一些端倪。
通过格式化 js 代码,得到
实际上,已经有大佬将计算 tk 代码破解了,这里我就直接放上来:
function token(a) {
var k = "";
var b = 406644;
var b1 = 3293161072;
var jd = ".";
var sb = "+-a^+6";
var Zb = "+-3^+b+-f";
for (var e = [], f = 0, g = 0; g < a.length; g++) {
var m = a.charCodeAt(g);
128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023),
e[f++] = m >> 18 | 240,
e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224,
e[f++] = m >> 6 & 63 | 128),
e[f++] = m & 63 | 128)
}
a = b;
for (f = 0; f < e.length; f++)
a += e[f],
a = RL(a, sb);
a = RL(a, Zb);
a ^= b1 || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + jd + (a ^ b)
};
function RL(a, b) {
var t = "a";
var Yb = "+";
for (var c = 0; c < b.length - 2; c += 3) {
var d = b.charAt(c + 2),
d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
d = b.charAt(c + 1) == Yb ? a >>> d: a << d;
a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d
}
return a
}
tk 都得到了,剩下的按部就班的就 OK 啦,JAVA 代码如下:
public class Google {
public static void main(String[] args) throws Exception {
String from = "zh-CN";
String to = "en";
String q = "我是谁,我在哪儿?";
String url = "http://translate.google.cn/translate_a/single";
String tk = token(q);
Map<String, String> params = new HashMap<String, String>();
params.put("client", "t");
params.put("sl", from);
params.put("tl", to);
params.put("hl", "zh-CN");
params.put("dt", "at");
params.put("dt", "bd");
params.put("dt", "ex");
params.put("dt", "ld");
params.put("dt", "md");
params.put("dt", "qca");
params.put("dt", "rw");
params.put("dt", "rm");
params.put("dt", "ss");
params.put("dt", "t");
params.put("ie", "UTF-8");
params.put("oe", "UTF-8");
params.put("source", "btn");
params.put("ssel", "0");
params.put("tsel", "0");
params.put("kc", "0");
params.put("tk", tk);
params.put("q", q);
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uri = new URIBuilder(url);
for (String key : params.keySet()) {
String value = params.get(key);
uri.addParameter(key, value);
}
HttpUriRequest request = new HttpGet(uri.toString());
CloseableHttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
System.out.println(result);
EntityUtils.consume(entity);
response.getEntity().getContent().close();
response.close();
}
private static String token(String value) {
String result = "";
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
try {
FileReader reader = new FileReader("Google.js");
engine.eval(reader);
if (engine instanceof Invocable) {
Invocable invoke = (Invocable)engine;
result = String.valueOf(invoke.invokeFunction("token", value));
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}