背景:需要为接口自动化做准备,拿到登录的令牌。
目标:模拟前端登录
-
登录逻辑:(1)现在前后端分离登录,一般登录逻辑采用AES加密密码+RSA加密向量和key,最后查询到redis中的验证码,将处理好的数据到后端进行验证。
摘自https://www.jianshu.com/p/360815e562e3
我们模拟的是红色圈主的部分逻辑
(2)涉及接口功能:验证码,与登录(获取公钥),登录
首先我们要先了解AES和RSA加密,介绍原理的东西比较多,可以参考以下链接
AES:https://www.cnblogs.com/vegetableDD/p/11866251.html
RSA:https://www.jianshu.com/p/ad3d1dea63af在开始之前我们需要的了解一些加密算法中必要的模式和规则,细则参考上面的两个链接。
AES的模式:这里使用CBC模式,CBC模式需要向量和key,两个都是十六位随机数
RSA的密钥格式:这里使用的是pkcs1
下面时代码:
随机生成十六位数的向量
# -*- coding: utf-8 -*-
import rsa
import time
import datetime
import random
def nu2():
t = time.time()
x= int(round(t * 1000))
y= random.randint(100,888)
p= str(x)+str(y)
return p
def nu3():
t = time.time ()
x = int (round (t * 1000))
z = random.randint (888, 999)
o = str (z) + str (x)
return o
def tag():
z = random.randint (1024, 10240)
return str(z)
if __name__ == '__main__':
print(nu3())
AES 与RSA加密方法
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
import base64
import re
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# 加密函数
def encrypt(text,key,iv):
key=key.encode ('utf-8')
mode = AES.MODE_CBC
iv = bytes(iv, encoding = "utf8")
text = add_to_16(text)
cryptos = AES.new(key, mode, iv)
cipher_text = cryptos.encrypt(text)
# 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为base64
return base64.encodebytes(cipher_text)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text):
key = '9999999999999999'.encode('utf-8')
iv = b'qqqqqqqqqqqqqqqq'
mode = AES.MODE_CBC
cryptos = AES.new(key, mode, iv)
plain_text = cryptos.decrypt(a2b_hex(text))
return bytes.decode(plain_text).rstrip('\0')
def Rsa_p(key,message):
rsakey = RSA.importKey (key)
cipher = Cipher_pkcs1_v1_5.new (rsakey)
cipher_text = base64.b64encode (cipher.encrypt (message.encode (encoding="utf-8"))) # 通过生成的对象加密message明文,注意,在python3中加密的数据必须是bytes类型的数据,不能是str类型的数据
return cipher_text
登录逻辑
import requests
import json
import JiaMi
import base64
import redis
import Nums
url_get = 'http://192.168.0.148:port/xx/(此处路径隐藏)'
tag= Nums.tag() #发送tag给获取验证码的接口
hearders={"tag":tag}
response = requests.get ('http://192.168.0.148:port/xx/(此处路径藏)',params=hearders)
x=response.content
conn = redis.Redis(host='192.168.0.148',port=6388,password='makenosense',db=0)#创建连接redis
#查找tag对应的验证码
x= conn.get("ai_building:"+tag)
x= x.decode()
# 模拟写验证码及tag作为参数
verifyCode = x
name = "ssy"
data = {"name": name, "verifyCode": verifyCode, "tag": tag}
data=json.dumps(data)
#发起预登陆
response1 = requests.post ('http://192.168.0.148:port/xx/(此处路径隐藏)', data)
f = response1.json ()
# 获取公钥
key = f['publicKey']
preSessionID =f['preSessionId']
#key经过编码,用base64解码,获得公钥
key= base64.b64decode(key)
#publickey_str = '-----BEGIN PUBLIC KEY-----\n'+key+'\n-----END PUBLIC KEY-----'
# 模拟输入密码
password = "ssy110"
#随机生成一下的AES的向量和key,必须是16位的随机数
authkey = Nums.nu2()
authSalt = Nums.nu3()
# 密码AES加密,因为要处理成字符串作为参数传入,所以这里要转成utf-8的字符串
password =str((JiaMi.encrypt (text=password, key=authkey, iv=authSalt)),'utf-8')
# 向量和key进行rsa加密,ras公钥通过之前获取
authKey = str(JiaMi.Rsa_p(key,authkey),'utf-8')
authSalt =str(JiaMi.Rsa_p(key, authSalt),'utf-8')
data1 = {"name": name, "preSessionID": preSessionID, "password": password, "authKey": authKey, "authSalt": authSalt}
#将data处理成json格式
data1=json.dumps(data1)
url= 'http://192.168.0.148:port/xx(此处路径隐藏)'
request = requests.post (url, data1)
result= request.json()
print(result)