python自动化测试-D5-学习笔记之二(常用模块之加密模块)

import hashlib #加密模块
#md5 加密是不可逆的,不能解密。网上的解密是 撞库 实现的,是去数据库里找已经存储的密码,所以输入123456这种简单的可以解密,输入复杂的就不能解密了,
sherry_pwd = 'sherry0423'
m = hashlib.md5() #
# print(dir(m)) #查询 m 的0所有方法
m.update(sherry_pwd.encode()) #加密,没有返回值,不能传字符串,只能传bytes类型,用encode() 把字符串转换成 bytes 类型 二进制
print(m.hexdigest()) #加密后的结果
# sha_256 加密
sha_256=hashlib.sha256(sherry_pwd.encode())
print(sha_256.hexdigest())
# sha_512 加密
sha_512=hashlib.sha512(sherry_pwd.encode())
print(sha_512.hexdigest())

def md5_password(st:str):# :str 是限定传入参数必须是字符串类型
bytes_st = st.encode()#转成二进制
m = hashlib.md5(bytes_st)#加密
return m.hexdigest() # 返回加密后的结果

res=md5_password('123456')
print(res)

import base64 # 可以加密,也可以解密, 例如网站url中可以把get请求的参数进行加密s
s = 'name'
byte_s = s.encode()#字符串变成二进制
r = base64.b64encode(byte_s)# base64 编码,注意是用的 encode
print(r.decode()) # 把r=b'x9dxa9x9e'转换成字符串
jiemi = base64.b64decode(r.decode())#解密,是用的 decode
print(jiemi.decode())


原文地址:https://www.cnblogs.com/blackbird0423/p/8322041.html