有道翻译采用js加密的方式,阻碍我们使用代码进行翻译。分析发送请求时提交的Form Data,数据中的salt(盐)和sign(签名)是常见的js加密方式,salt是一个很长的随机串,是防止程序员使用字典反推({明文:密文}--{密文:明文})的形式破解加密方式(增加了原字符串的数量,使破解难以完成)。sign也是一种js加密方式。
sign的加密方式使用了md5加密算法,即消息摘要算法第五版,提供消息的完整性保护,由js代码可知md5()的参数是一个由四个字符串组成的字符串,第一个和第二个都是常字符串,第三个是所谓的salt,第四个是输入要翻译的单词;salt的加密方式是当前时间取整*1000然后再加上0~10之间的一个随机数而得。下面直接上代码:
[Python] 纯文本查看 复制代码 import hashlib
import json
import random
import time
import requests
def youdao():
content = input("请输入您要翻译的内容:")
u = 'fanyideskweb'
d = content
# 生成salt
salt = str(int(time.time() * 1000) + random.randint(1, 10))
c = 'p09@Bn{h02_BIEe]$P^nG'
# 生成sign
sign = hashlib.md5((u + d + salt + c).encode('utf-8')).hexdigest()
url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
data = {
"i": content,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": salt,
"sign": sign,
# "ts": "1547536652328",
# "bv": "363eb5a1de8cfbadd0cd78bd6bd43bee",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_REALTIME",
"typoResult": "false",
}
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Language": "zh-CN,zh;q = 0.9",
"Connection": "keep-alive",
"Content-Length": "271",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": "OUTFOX_SEARCH_USER_ID=-1105037136@10.168.8.64; JSESSIONID=aaadY5kjkBpKngxtXrqHw; OUTFOX_SEARCH_USER_ID_NCOO=1440145660.3035378; ___rl__test__cookies=1547536652325",
"Host": "fanyi.youdao.com",
"Origin": "http://fanyi.youdao.com",
"Referer": "http://fanyi.youdao.com/",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
}
response = requests.post(url, data=data, headers=headers)
print(json.loads(response.content.decode()).get("translateResult")[0][0].get("tgt"))
print(response.content.decode())
if __name__ == '__main__':
youdao()
|