python学习笔记(十四)加密模块

 1 import hashlib
 2 ybq_pwd='bugaosuni'
 3 m=hashlib.md5()
 4 bytes_ybq=ybq_pwd.encode()#把字符串转成bytes类型,中文字符在Python中是以unicode存在的,我们在进行hash前必须把数据转换成bytes类型
 5 m.update(bytes_ybq)#加密,不能字符串,只能传byte类型,二进制
 6 print(m.hexdigest())#加密后的结果
 7 
 8 #md5加密是不可逆的,不能被解密的
 9 #撞库
10 def md5_password(st:str):
11     bytes_st=st.encode()#转成二进制类型
12     m=hashlib.md5(bytes_st)#加密
13     return m.hexdigest()#返回加密后的结果
14 res=md5_password('123456')
15 print(res)
16 
17 
18 sha256=hashlib.sha256(bytes_ybq)
19 print(sha256.hexdigest())
1 import base64
2 s='hahaha'
3 byte_s=s.encode()#字符串变成二进制
4 res=base64.b64encode(byte_s)#base64编码
5 print(res.decode())#把byte转成字符串
6 
7 jie_mi_res=base64.bs64decode(res.decode())
8 print(jie_mi_res.decode())
原文地址:https://www.cnblogs.com/wxcx/p/8299311.html