此程序的要求是随机生成一个用户名和一个密码,存入txt文档中,然后取出来把密码加密后再次存储。主要练习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位数的用户名。
请发表评论