百度翻译接口 破解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hujingshuang/article/details/80180294

前面介绍了 《有道翻译接口 破解》和《谷歌翻译接口 破解》,下面继续尝试一下百度翻译(Github地址,欢迎star)。
同样,查看首页元素,如下:
审查元素
可以得到其 Request URL 和 Request Method,再往下看:
审查元素
需要我们注意的是,sign 和 token 这两个参数,应该如何设置它们?接下来,继续研究。我们在 js 代码中用 token作为关键字进行搜索,然后发现在某个 index_xxx 开头的 js 代码中发现一段可疑代码。
审查元素
拷贝这个 js 代码,经过格式化,搜索 token 可以找到代码段。
js 代码1
js 代码2
说明 token 是通过 window.common.token 来获取的,而 sign 是利用待翻译的内容q并通过 m 函数得到的,通过搜索得到 m 函数的 js 代码,如下:
m 函数
这里将其整理一下得到可用的 js 代码,如下:

function a(r, o) {
    for (var t = 0; t < o.length - 2; t += 3) {
        var a = o.charAt(t + 2);
        a = a >= "a" ? a.charCodeAt(0) - 87 : Number(a),
        a = "+" === o.charAt(t + 1) ? r >>> a: r << a,
        r = "+" === o.charAt(t) ? r + a & 4294967295 : r ^ a
    }
    return r
}
var C = null;
var token = function(r, _gtk) {
    var o = r.length;
    o > 30 && (r = "" + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substring(r.length, r.length - 10));
    var t = void 0,
    t = null !== C ? C: (C = _gtk || "") || "";
    for (var e = t.split("."), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) {
        var m = r.charCodeAt(g);
        128 > m ? d[f++] = m: (2048 > m ? d[f++] = m >> 6 | 192 : (55296 === (64512 & m) && g + 1 < r.length && 56320 === (64512 & r.charCodeAt(g + 1)) ? (m = 65536 + ((1023 & m) << 10) + (1023 & r.charCodeAt(++g)), d[f++] = m >> 18 | 240, d[f++] = m >> 12 & 63 | 128) : d[f++] = m >> 12 | 224, d[f++] = m >> 6 & 63 | 128), d[f++] = 63 & m | 128)
    }
    for (var S = h,
    u = "+-a^+6",
    l = "+-3^+b+-f",
    s = 0; s < d.length; s++) S += d[s],
    S = a(S, u);

    return S = a(S, l),
    S ^= i,
    0 > S && (S = (2147483647 & S) + 2147483648),
    S %= 1e6,
    S.toString() + "." + (S ^ h)
}

另外,在 html 源码中搜索了一番,找到了 token 和 gtk。
html 源码
HTML 源码
因此,通过上述分析,便得到了 token 和 sign 参数值了。按照格式发起请求,代码如下:

public class BaiDu {
    public static void main(String[] args) throws Exception{
        String from = "zh";
        String to = "en";
        String q = "小猪佩奇身上纹,掌声送给社会人!";
        String token = "853eba4bc7ac9601ef8fff761c7cf8cf";
        String gtk = "320305.131321201";
        String url = "http://fanyi.baidu.com/v2transapi";
        String sign = token(q, gtk);

        Map<String, String> params = new HashMap<String, String>();
        params.put("from", from);
        params.put("to", to);
        params.put("query", q);
        params.put("transtype", "translang");
        params.put("simple_means_flag", "3");
        params.put("sign", sign);
        params.put("token", token);

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpPost request = new HttpPost(url);
        request.setEntity(new UrlEncodedFormEntity(util.convertParams(params), "UTF-8"));
        request.setHeader("Cookie", "BAIDUID=BBAF98B01ABDC86AE4980FBD0367379A:FG=1; PSTM=1524640041; BIDUPSID=841D64086F3EC20B99798BB575920DC1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1458_19033_21091_18560_20927; PSINO=3; locale=zh; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1524634369,1524719015,1524734535,1525326108; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1525329053");
        request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

        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 sign) {
        String result = "";
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
        try {
            FileReader reader = new FileReader("Baidu.js");
            engine.eval(reader);

            if (engine instanceof Invocable) {
                Invocable invoke = (Invocable)engine;
                result = String.valueOf(invoke.invokeFunction("token", value, sign));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

注意:
1、为方便演示,在上述代码中直接指定了 gtk 和 token。但二者通常隔一段时间就会发生改变,所以一劳永逸的办法是在发起请求前,先获取 http://fanyi.baidu.com/ 的 HTML 源码,然后从该源码中提取出 gtk 和 token,最后作为请求参数;
2、需要设置 cookie(上述代码也直接固定了Cookie,当 Cookie 失效后就不能继续用了;同理,实际上在发起请求前应先获取 Cookie 再设置),否则会出现 error。错误代码:997,没有cookie; 998,cookie 过期;999,内部错误。

猜你喜欢

转载自blog.csdn.net/hujingshuang/article/details/80180294