看别人的文章,抄成自己的,我用的是python3
简单了解是怎么用:
import hashlib hash = hashlib.md5() # md5对象,md5不能反解,但是加密是固定的,就是关系是一一对应,所以有缺陷,可以被对撞出来 hash.update(bytes('admin', encoding='utf-8')) # 要对哪个字符串进行加密,就放这里 print(hash.hexdigest()) # 拿到加密字符串
结果如下:
1 21232f297a57a5a743894a0e4a801fc3
用户密码加密保存:
import hashlib def md5(arg): # 这是加密函数,将传进来的函数加密 md5_pwd = hashlib.md5(bytes('abd', encoding='utf-8'))#加盐,防止解密 md5_pwd.update(bytes(arg, encoding='utf-8')) return md5_pwd.hexdigest() # 返回加密的数据 def log(user, pwd): # 登陆时候时候的函数,由于md5不能反解,因此登陆的时候用正解 with open('db', 'r', encoding='utf-8') as f: for line in f: u, p = line.strip().split('|') if u == user and p == md5(pwd): # 登陆的时候验证用户名以及加密的密码跟之前保存的是否一样 return True def register(user, pwd): # 注册的时候把用户名和加密的密码写进文件,保存起来 with open('db', 'a', encoding='utf-8') as f: temp = user + '|' + md5(pwd) f.write(temp) i = input('1表示登陆,2表示注册:') if i == '2': user = input('用户名:') pwd = input('密码:') register(user, pwd) elif i == '1': user = user = input('用户名:') pwd = input('密码:') r = log(user, pwd) # 验证用户名和密码 if r == True: print('登陆成功') else: print('登陆失败') else: print('账号不存在')