爬虫 之 RSA(JSEncrypt)加密

时间:2024-10-29 16:14:56
import base64 from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5 from Crypto.PublicKey import RSA def encrpt(password, public_key): # password :待加密字符串 public_key:公钥 rsakey = RSA.importKey(public_key) cipher = Cipher_pksc1_v1_5.new(rsakey) cipher_text = base64.b64encode(cipher.encrypt(password.encode())) return cipher_text.decode() def main(): sign = str(int(time.time())) + '000' password = encrpt(sign, public_key) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36', 'sign': password, 'Cookie': "_gscu_872847596=09224904520vb611; _gscbrs_872847596=1", 'Content-Type': 'application/json;charset=UTF-8', 'timestamp': sign # 关键字段 ,后端应该拿着私钥进行解密,与此字段比对的 } data = { 'fzjg': "", 'page': page, 'rows': 50, 'zszl': "00103" } response = "" while 1: try: time.sleep(1.3) response = requests.request(method='post', url=url, headers=headers,json=data,proxies=proxys[-1],timeout=10) if response.status_code == 200: response = response.json() code = response['code'] if code == 200: print('获取信息成功!!!') print('break!!!') break except Exception as e: print(e) dl() pass