md5和base64加密
- md5加密特点:密文长度固定 ;不能够被反解析
- base64加密特点:密文长度不固定 ;能够被反解析
import hashlib
a = '柳柳'
# b = '123456789'
res_a = hashlib.md5(a.encode()).hexdigest()#用md5加密,md5接受byte类型;hexdigest()加密后的16进制的字符串
# res_b = hashlib.md5(b.encode()).hexdigest()
print(res_a)
import base64
a = '123A'
res_a = base64.b64encode(a.encode()).decode()
print(res_a) # MTIzQQ==
"""
反解析
"""
a = "MTIzQQ=="
res_a = base64.b64decode(a.encode()).decode()
print(res_a) # 123
案例1:有道翻译
import requests
import hashlib
import random
import time
import json
if __name__ == '__main__':
while True:
data_ = input('请输入你想要翻译的东西:')
# 确认url
url_ = "https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
# 请求头
headers_ = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36",
"Referer": "https://fanyi.youdao.com/",
"Cookie": "OUTFOX_SEARCH_USER_ID=265786532@10.169.0.81; OUTFOX_SEARCH_USER_ID_NCOO=879005324.6578022; _ntes_nnid=6d3c8f32846ae1ebc9845de680fca423,1628772129772; fanyi-ad-id=118539; fanyi-ad-closed=1; JSESSIONID=aaa0KI0Fqer-n521T0w1x; ___rl__test__cookies=1637842547929"
}
# lts:以毫秒为单位的时间戳(格林尼治时间)
time_ = str(int(time.time() * 1000))
# salt:lts随机加盐
salt_time = time_ + str(random.randint(0,9))
# sign值的生成
a = "fanyideskweb" + data_ + salt_time + "Ygy_4c=r#e#4EX^NUGUc5"
sign_ = hashlib.md5(a.encode()).hexdigest()
# 构造form表单
form_data = {
"i":data_ ,
"from": "AUTO",
"to": "AUTO",
"smartresult": "dict",
"client": "fanyideskweb",
"salt": salt_time,
"sign": sign_,
"lts": time_,
"bv": "eda468fc64295ecf2810ab8a672c2db1",
"doctype": "json",
"version": "2.1",
"keyfrom": "fanyi.web",
"action": "FY_BY_CLICKBUTTION"
}
try:
# 发送请求,得到响应
response_ = requests.post(url_,headers=headers_,data=form_data)
res_ = json.loads(response_.content.decode())['translateResult']
print(f'翻译结果是:{res_}')
# # 延迟
# time.sleep(1)
except:
print("输入有误,请重新输入.....")
continue
执行JS代码得到参数
1.需要 node.js
--根据电脑版本官网下载,解压,安装Download | Node.js (nodejs.org)
--路径添加到系统环境变量 终端输入 node -v
--需要是专业版本的 pro > Pycharm
-- settings > plug-in > 搜索node.js 安装即可
-- settings - Languages & Frameworks - Node.js and NPM里面,配置好前面安装的node.js的路径即可
2.下载一个第三方库 PyExecJS pip install PyExecJS -i https://pipy.doubanio.com/simple
//demo_1
function add(a,b) {
return a+b
}
import execjs
# 1.使用文件的读写拿到js文件里面的代码
with open('demo_1.js','r') as f:
js_data = f.read()
# 2.拿到js的代码时候,需要进行一个类似编码的操作
js_obj = execjs.compile(js_data)
# 3.执行js代码,以字符串的形式把函数名称放进去
res_ = js_obj.call('add',20,7)
print(res_) # 27
案例2:百度翻译
分析过程:
1.测试后发现表单数据只有sign是动态的。全局搜索sign,由于携带sign的js文件过多,选择表单另一个参数simple_means_flag进行搜索
2.打上断点,启动程序,发现sign值疑似由一个b函数执行得到,参数为要被翻译的数据query
3.断点打在函数调用部分,这样就可以进入函数里面看它的执行代码
4.右上角点击向下的箭头(F11)进入函数定义部分
5.函数内部,执行按右上角向右的箭头,快捷键为F9
6.复制对应的js代码块,到本地文件(可以复制对应的部分,实在不行复制全部)
7.出现未知错误,可以对js代码进行一定程序的删减
8.报错i没有被定义: window is not defined
--python代码
--js代码 >> 使用window 却没有定义window,通过控制台知道window[d]是个固定值,即'320305.131321201',直接在js里面赋值即可。如果是动态参数,还需要获得动态参数的生成函数
// baidu.js
function n(t, e) {
for (var n = 0; n < e.length - 2; n += 3) {
var r = e.charAt(n + 2);
r = "a" <= r ? r.charCodeAt(0) - 87 : Number(r),
r = "+" === e.charAt(n + 1) ? t >>> r : t << r,
t = "+" === e.charAt(n) ? t + r & 4294967295 : t ^ r
}
return t
}
var r = null;
function s(t) {
var o, i = t.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g);
if (null === i) {
var a = t.length;
a > 30 && (t = "".concat(t.substr(0, 10)).concat(t.substr(Math.floor(a / 2) - 5, 10)).concat(t.substr(-10, 10)))
} else {
for (var s = t.split(/[\uD800-\uDBFF][\uDC00-\uDFFF]/), c = 0, u = s.length, l = []; c < u; c++)
"" !== s[c] && l.push.apply(l, function(t) {
if (Array.isArray(t))
return e(t)
}(o = s[c].split("")) || function(t) {
if ("undefined" != typeof Symbol && null != t[Symbol.iterator] || null != t["@@iterator"])
return Array.from(t)
}(o) || function(t, n) {
if (t) {
if ("string" == typeof t)
return e(t, n);
var r = Object.prototype.toString.call(t).slice(8, -1);
return "Object" === r && t.constructor && (r = t.constructor.name),
"Map" === r || "Set" === r ? Array.from(t) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? e(t, n) : void 0
}
}(o) || function() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}()),
c !== u - 1 && l.push(i[c]);
var p = l.length;
p > 30 && (t = l.slice(0, 10).join("") + l.slice(Math.floor(p / 2) - 5, Math.floor(p / 2) + 5).join("") + l.slice(-10).join(""))
}
for (var d = "".concat(String.fromCharCode(103)).concat(String.fromCharCode(116)).concat(String.fromCharCode(107)), h = (null !== r ? r : (r = '320305.131321201' || "") || "").split("."), f = Number(h[0]) || 0, m = Number(h[1]) || 0, g = [], y = 0, v = 0; v < t.length; v++) {
var _ = t.charCodeAt(v);
_ < 128 ? g[y++] = _ : (_ < 2048 ? g[y++] = _ >> 6 | 192 : (55296 == (64512 & _) && v + 1 < t.length && 56320 == (64512 & t.charCodeAt(v + 1)) ? (_ = 65536 + ((1023 & _) << 10) + (1023 & t.charCodeAt(++v)),
g[y++] = _ >> 18 | 240,
g[y++] = _ >> 12 & 63 | 128) : g[y++] = _ >> 12 | 224,
g[y++] = _ >> 6 & 63 | 128),
g[y++] = 63 & _ | 128)
}
for (var b = f, w = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(97)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(54)), k = "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(51)) + "".concat(String.fromCharCode(94)).concat(String.fromCharCode(43)).concat(String.fromCharCode(98)) + "".concat(String.fromCharCode(43)).concat(String.fromCharCode(45)).concat(String.fromCharCode(102)), x = 0; x < g.length; x++)
b = n(b += g[x], w);
return b = n(b, k),
(b ^= m) < 0 && (b = 2147483648 + (2147483647 & b)),
"".concat((b %= 1e6).toString(), ".").concat(b ^ f)
}
import execjs
import requests
import json
if __name__ == '__main__':
url = 'https://fanyi.baidu.com/v2transapi'
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.52",
'Cookie': "PSTM=1656208998; BIDUPSID=2577780AE63B20ADB45D81A07C5910AD; BAIDUID=2577780AE63B20ADC2A663257BED8381:FG=1; BAIDUID_BFESS=2577780AE63B20ADC2A663257BED8381:FG=1; ZFY=jat7e18771g5toEB:A2tFa38i45tmDxWfRsYd0hcQvfw:C; BA_HECTOR=2h25al2584ah0h8h24216o6m1hl6nae1a; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BDRCVFR[-BxzrOzUsTb]=mk3SLVN4HKm; H_PS_PSSID=26350; APPGUIDE_10_0_2=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1666423084; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1666423114; ab_sr=1.0.1_YTA5OTA0MGM2YWU1YjE3MDU5MDIxN2Y2YmQwODg1NWI2YmZiNTAzNDQ1NWJmNGFmYWFhN2YyNDQ1YTE2OWMwNzYzZWUzZDgzMWEyNWMwZjY1NzE4ZDJhZmFlYTdhMDNmODgwYzQ5ZWVjMzZkMDRkMzdlZGEwN2NjYjk3MmU4MDUyYTgzMWViNTM3Njk3OTQ3ZmZkNDQ3OGNjOWQxNDA4Ng==",
'Referer': "https://fanyi.baidu.com/"
}
data = input("翻译:")
with open('baidu.js', 'r') as f:
js_data = f.read()
js_obj = execjs.compile(js_data)
sign = js_obj.call('s', data)
form_data = {
'from': 'zh',
'to': 'en',
'query': data,
'transtype': 'enter',
'simple_means_flag': '3',
'sign': sign,
'token': 'd49d7fecb829d5912e8a956d15ddf1e5',
'domain': 'common'
}
result = requests.post(url, headers=headers,data=form_data)
print(json.loads(result.content)['trans_result']['data'][0]['dst'])