金典小程序之密碼加密

此程序的要求是隨機生成一個用戶名和一個密碼,存入txt文檔中,而後取出來把密碼加密後再次存儲。主要練習python文件的操做。python

import re import random import hashlib charactor='abcdefghijklmnopqrstuvwxyz0123456789' len_char = len(charactor)-1
#生成名字
list=[] count = 0 while count<4: for i in charactor[random.randint(0,len_char)]: list.append(i) count+=1 name = ''.join(list) print('name',name) #生成密碼
list_password=[] count = 0 while count<6: for i in charactor[random.randint(0,len_char)]: list_password.append(i) count += 1 password = ''.join(list_password) print('password',password) save = open(r'C:\Users\jeep-peng zhang\Desktop\2.txt','w',encoding='gbk') save.write(name+','+password+'\n') save.close() #加密
f = open(r'C:\Users\jeep-peng zhang\Desktop\2.txt','r',encoding='gbk') password_hash= f.readline().strip().split(',') password_hash[1]= password_hash[1].encode('gb2312')#注意:hash要在unnicode編碼前進行
hash_read = hashlib.md5(password_hash[1]).hexdigest() f=open(r'C:\Users\jeep-peng zhang\Desktop\md5.txt','w',encoding='gbk') f.write(password_hash[0]+','+hash_read+'\n') f.close()
while count<4: for i in charactor[random.randint(0,len_char)]: list.append(i) count+=1 name = ''.join(list) print('name',name)此程序的做用是循環4次生成一個隨機的4位數的用戶名。
相關文章
相關標籤/搜索