百度翻譯介面破解
阿新 • • 發佈:2021-11-29
百度翻譯網頁地址
程式碼
- 獲取簽名
import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; /** * @author xiaog */ public class Sign { private Sign() { } public static ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); /** * 呼叫js方法生成簽名 * @param query 原文 * @param gtk 種子簽名 * @return 簽名 * @throws Exception */ public static String getSign(String query, String gtk) throws ScriptException, NoSuchMethodException { String func = "function getSign(query) {\n" + " function a(r, o) {\n" + " for (var t = 0; t < o.length - 2; t += 3) {\n" + " var a = o.charAt(t + 2);\n" + " 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\n" + " }\n" + " return r\n" + " }\n" + "\n" + " var C = null;\n" + " var hash = function (r, _gtk) {\n" + " var o = r.length;\n" + " o > 30 && (r = '' + r.substr(0, 10) + r.substr(Math.floor(o / 2) - 5, 10) + r.substr(-10, 10));\n" + " var t = void 0, t = null !== C ? C : (C = _gtk || '') || '';\n" + " for (var e = t.split('.'), h = Number(e[0]) || 0, i = Number(e[1]) || 0, d = [], f = 0, g = 0; g < r.length; g++) {\n" + " var m = r.charCodeAt(g);\n" + " 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)\n" + " }\n" + " for (var S = h, u = '+-a^+6', l = '+-3^+b+-f', s = 0; s < d.length; s++) S += d[s], S = a(S, u);\n" + " return S = a(S, l), S ^= i, 0 > S && (S = (2147483647 & S) + 2147483648), S %= 1e6, S.toString() + '.' + (S ^ h)\n" + " }\n" + " return hash(query,'" + gtk + "')\n" + "}\n"; //執行js指令碼定義函式 engine.eval(func); Invocable invocable = (Invocable) engine; Object res = invocable.invokeFunction("getSign", query); return res.toString(); } }
- 請求翻譯介面
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Setter; import okhttp3.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; public class Translate { private static ObjectMapper objectMapper = new ObjectMapper(); private static OkHttpClient client = new OkHttpClient.Builder().build(); /** * 令牌 window.common.token */ private static String token = "925160a8745a657e443b666ce5d3c383"; /** * cookie */ private static String cookie = "BAIDUID=FAE39815F9431170B8E230B87980567A:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1638152216; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1638152217; __yjs_duid=1_edd46d1edb3a155419bbb51bcaf3a6941638152218380; ab_sr=1.0.1_NDgyOWM1NDgzNDJlMDNlYzVhNGJlOTcxMjBmZGQ0NGFiZDdiOTUzNGEwZDQ0MWIyMTcyMmQzNTZjMGMxMGU3OTQ3OGEzNWJhNGJkZDQ3ODM0YzUxOGJjMGZkOTEyNGU0NmJlYzJjNWFmZTI0ZTg3NmYwODYzMjY4YjhhMmRhZGZhY2U0YTA5NTMzNDBjZmRiNzJmMGJkMTM0OTJlYjRmZg=="; /** * 種子簽名 window.gtk */ private static String seedSign = "320305.131321201"; private static RequestBody getRequestBody(String query) throws Exception { String sign = Sign.getSign(query, seedSign); return new FormBody.Builder() .add("from", "en") .add("to", "zh") .add("query", query) .add("transtype", "realtime") .add("simple_means_flag", "3") .add("sign", sign) .add("token", token) .add("domain", "common") .build(); } private static Request getRequest(RequestBody requestBody) { return new Request.Builder() .url("https://fanyi.baidu.com/v2transapi?from=en&to=zh") .header("Connection", "keep-alive") .header("sec-ch-ua", "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"") .header("Accept", "*/*") .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .header("X-Requested-With", "XMLHttpRequest") .header("sec-ch-ua-mobile", "?0") .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36") .header("sec-ch-ua-platform", "\"Windows\"") .header("Origin", "https://fanyi.baidu.com") .header("Sec-Fetch-Site", "same-origin") .header("Sec-Fetch-Mode", "cors") .header("Sec-Fetch-Dest", "empty") .header("Referer", "https://fanyi.baidu.com/") .header("Accept-Language", "zh-CN,zh;q=0.9") .header("Cookie", cookie) .post(requestBody).build(); } public static String enToZh(String query) throws Exception { RequestBody requestBody = getRequestBody(query); Request request = getRequest(requestBody); Response response = client.newCall(request).execute(); String result = response.body().string(); JsonNode jsonNode = objectMapper.readTree(result); String res = jsonNode.at("/trans_result/data/0/dst").asText(); return res; } public static void main(String[] args) throws Exception { String result = enToZh("hello world"); System.out.println(result); } }
- 依賴
<dependencies> <!-- jackson,用於處理json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version> </dependency> <!-- okhttp3,用於傳送http請求 --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.1</version> </dependency> </dependencies>
解釋
- 以下是百度翻譯介面的完整HTTP請求,格式是idea的http檔案格式
POST https://fanyi.baidu.com/v2transapi?from=en&to=zh
Connection: keep-alive
sec-ch-ua: "Chromium";v="94", "Google Chrome";v="94", ";Not A Brand";v="99"
Accept: */*
X-Requested-With: XMLHttpRequest
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36
sec-ch-ua-platform: "Windows"
Origin: https://fanyi.baidu.com
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://fanyi.baidu.com/
Accept-Language: zh-CN,zh;q=0.9
Cookie: BAIDUID=D7029489CFEEAB420F853746FCA71824:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1637976767; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1637976767; __yjs_duid=1_0103d0111a955a8b1616dbf9b12fe69f1637976760950; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; __yjs_st=2_MmI4YWY1M2U4OGI3MzEzM2U4YmYxMjYwYjA3NTQ5ZWQ0M2MxMDVlYjVmMGJkMTIxYTBiY2FjMTExYzEyMjJlZDc3MGEwN2U2Nzk0YjdjODc0NzM2NGI4YzQ3ZmZkNjcwZTc3NzhlZWVjMjRlNDRlMTMzMDVjNDZiYjZlM2M5ZTA0MDJkNDEwMjkyYzJiZjU5N2QxODY4NTVjODNmNGJiMmNlYzg4NWU4NjIxZWMwYTM5Y2RmODdmY2ZiOWQzYzk1MzA5ZGM2ZDU4ODNkNjY4OWU5NmQxNzA5YjQ2NjQwNmQzYzFhMmViNmU4ZDAwNmFmMWJjMTYwMzNhNjkxY2Q3MF83X2QxOTg4MjYx; ab_sr=1.0.1_MTZlMTk3YjY5YmMyNWNjZTJmMTdhMGFlYmM5MWY2NjMyOGVmMzE1YzYyYzY4ZjE2OGUxYmVjMTBjNzM2NWY1ZWQ1MDExYzE2ZTAyMTRjNTc2OGJlMDhhZjAzZjhmODYwOGI5NmU4MGE3MDg3NmMzYzhlMWM3MGNlODA1MDM4Mzk4NzdiYWM4ZGNjMDY1YmFiOGZmZGNjZTRjNjUwZjcxMQ==
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
from=en&to=zh&query=hello&transtype=realtime&simple_means_flag=3&sign=54706.276099&token=1bf7c3c20520a2203e4668c83073bfba&domain=common
- 在以上請求中有如下變數
cookie
from
,原文的語種簡稱,英文是en
to
,譯文的語種簡稱,中文是zh
query
,譯文sign
,簽名,原網頁中是在js中生成,需要一個種子簽名,可在頁面通過window.gtk
獲取token
,令牌,原網頁中的變數,可在頁面通過window.common.token
獲取
- 程式碼中的Sign類就是網頁中生成sign的js方法,利用java去執行