目录
MD5加密
- 封装加密方法
python
def md5_encode(self,data):
data = str(data).encode("utf-8")
md5_data = hashlib.md5(data).hexdigest()
return md5_data
- 写入需要使用加密的接口yaml用例
python
-
request:
method: post
url: http://192.168.0.1/md5login
data:
username: ${md5_encode(admin)} # 这里直接调用MD5的方法即可
password: ${md5_encode(123)}
base64加密
- 封装加密方法
python
def base64_encode(self,data):
data = str(data).encode("utf-8")
base64_data = base64.b64encode(data).decode("utf-8")
return base64_data
- 写入需要使用加密的接口yaml用例
python
-
request:
method: post
url: http://192.168.0.1/base64login
data:
username: ${base64_encode(admin)}
password: ${base64_encode(123)}
rsa加密
需要先进行下载 pip install rsa
- 生成rsa使用的公钥和私钥
python
def create_key(self):
# 生成1024位的公钥和私钥-RSA密钥至少为500位,密钥长度随着保密级别提高
public_key,private_key = rsa.newkeys(1024)
# 将生成的公钥和密码写入文件
with open("./public_key.pem","w+") as f :
f.write(public_key.save_pkcs1().decode())
with open("./private_key.pem","w+") as f :
f.write(private_key.save_pkcs1().decode())
- 封装加密方法
这里读取的时候注意相对路径的问题
python
def rsa_encode(self,data):
# 读取公钥
with open("./public_key.pem") as f:
public_key = rsa.PublicKey.load_pkcs1(f.read().encode())
# 把data转为utf-8格式
data = str(data).encode("utf-8")
# 把字符串加密成byte类型
byte_data = rsa.encrypt(data,public_key)
# 把字节转为字符串
base64_data = base64.b64encode(byte_data).decode("utf-8")
- 写入需要使用加密的接口yaml用例
python
-
request:
method: post
url: http://192.168.0.1/rsalogin
data:
username: ${rsa_encode(admin)}
password: ${rsa_encode(123)}