纵有疾风起
人生不言弃

(python)RSA加密网站模拟登陆

标签: RSA js python 模拟登陆


模拟登陆之后就可以通过cookie保持登陆状态,也可以用于记住密码登陆,可以用于快速抢课,快速爬取课程,该文章只用于技术交流请勿用于违法违纪的行为

1.前端提交form之前call后台,要求获得publicKey的相关参数。

login.js

var modulus,exponent;//_path='http://jw.jluzh.com'$.getJSON(_path+"/xtgl/login_getPublicKey.html?time="+new Date().getTime(),function(data){        modulus = data["modulus"];        exponent = data["exponent"];});
2.后台生成密钥对,把privateKey存在session,把publicKey的modulus和exponent返回前台。
3.前台用publicKey对password加密

login.js

var rsaKey = new RSAKey();rsaKey.setPublic(b64tohex(modulus), b64tohex(exponent));var enPassword = hex2b64(rsaKey.encrypt($("#mm").val()));        $("#mm").val(enPassword);        $("#hidMm").val(enPassword); 
4.把form提交到后台,后台使用privateKey对密码进行解密:
String password = RSAUtils.decrypt(privateKey, enPassword);
5.将解密后的password进行加密,存入数据库或者和数据库中的数据进行比对:
DigestUtils.md5Hex(password);
6.python3 代码实现

(1)依赖.

pip install requests base64 rsa bs4

(2)知识准备

参考:爬虫-python requests

(3)完整代码

import requestsimport base64import rsafrom bs4 import BeautifulSoup as bsyhm='学号'url='http://jw.jluzh.com/xtgl/login_slogin.html'mm=b('密码')session = requests.Session()#获取公钥需要的参数publickey = session.get('http://jw.jluzh.com/xtgl/login_getPublicKey.html').json()b_modulus=base64.b64decode(publickey['modulus'])#将base64解码转为bytesb_exponent=base64.b64decode(publickey['exponent'])#将base64解码转为bytes#公钥生成,python3从bytes中获取int:int.from_bytes(bstring,'big')mm_key = rsa.PublicKey(int.from_bytes(b_modulus,'big'),int.from_bytes(b_exponent,'big'))#利用公钥加密,bytes转为base64编码rsa_mm = base64.b64encode(rsa.encrypt(mm, mm_key))page = session.get(url)soup = bs(page.text,"html.parser")#获取认证口令csrftokencsrftoken = soup.find(id="csrftoken").get("value")postdata={'csrftoken':csrftoken,'yhm':yhm,'mm':rsa_mm}f = open('test.html','wb')rq=session.post(url,data=postdata)f.write(rq.content)f.close()

注意:网络字节顺序是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释。网络字节顺序采用big endian(大端)排序方式。

文章转载于:https://www.jianshu.com/p/4c52134a5e35

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » (python)RSA加密网站模拟登陆
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录