Python 随机生成高强度密码

"""
该源码的实现过程:1、这年头,几十个账户,几十个密码,每次一设置密码就头疼,不知道设置啥。
                设置简单容易被暴力破解,设置复杂不知道设置成啥样的密码。
                所以就想出来设置随机数密码,自动生成,又能防止暴力破解。
               2、最大的优点也是缺点,人是记不住随机数的,所以密码备份就显得特别重要。
                特别是对该密码的解释,不然你回头找密码的时候,发现这个密码不知道对应哪个账号。
                3、1.0版本用了黑窗,非常不美观,这次用了窗体。
"""

import os
import random
import easygui as eg

# 字母类型
englishChar = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 'k', 'j', 'h', 'g', 'f', 'd', 's', 'a', 'z', 'x',
               'c', 'v',
               'b', 'n', 'm']
# 数字类型
numberChar = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

# 符号类型
symbolChar = ['!', '@', '#', '$', '%', '^', '&', '*']

# 生成的密码
password = ''

# 用户选择的密码类型
allChar = []

# 选择密码类型,如果选择cancel退出程序,啥都不选退出程序
typePassword = eg.multchoicebox(msg="密码类型", title="随机密码", choices=['数字', '字母', '符号'],
                                preselect=0, callback=None,
                                run=True)
# 退出程序
if not isinstance(typePassword, list):
    exit(1)

# 根据密码类型拼接密码库
for i in (typePassword):
    if i.__eq__('数字'):
        allChar += numberChar.copy()
    if i.__eq__('字母'):
        allChar += englishChar.copy()
    if i.__eq__('符号'):
        allChar += symbolChar.copy()

# 把密码打乱
random.shuffle(allChar)

# 设置密码长度
passwordLength = eg.integerbox(msg="密码的长度(9-25)", title="随机密码", default=None,
                               lowerbound=9, upperbound=25, image=None, root=None)
# 退出程序
if not isinstance(passwordLength, int):
    exit(1)
# 配置密码的信息
while 1:
    chooses = eg.multenterbox(msg="该密码的信息", title="随机密码",
                              fields=['这个密码给谁用?', '该密码的账号'], callback=None, run=True)
    # 退出程序
    if not isinstance(chooses, list):
        exit(1)
    # 信息满足要求  退出循环  进入下一步
    if not '' in chooses:
        break
    else:
        # 不满足要求
        eg.msgbox(msg="信息不能设置为空,请重新输入", title="随机密码",
                  ok_button="OK", image=None, root=None)
# 获得密码
for i in range(int(passwordLength)):
    # 每次循环随机取一位密码
    password = password + allChar[random.randint(0, len(allChar) - 1)]
eg.msgbox(msg="因为密码是随机的,需要备份密码,接下来会选择存放备份密码的文件夹。", title="随机密码",
          ok_button="OK", image=None, root=None)
          
# 选择文件保存路径
path = eg.diropenbox(msg="备份密码的文件", title="随机密码", default="")

# 拼接文件路径
passwordPath = path + '/' + chooses[0] + '的账户和密码.txt'

# 如果文件重复  文件结尾拼接_repeat
while 1:
    if os.path.exists(passwordPath):
        passwordPath = passwordPath[:-4] + '_repeat.txt'
    else:
        break

# 把账号密码写入文件,进行备份
with open(passwordPath, 'w', encoding='utf8') as file:
    file.writelines("账户ID:" + chooses[1] + '\n')
    file.writelines('密码:' + password)
    file.close

# 展示密码
eg.enterbox(msg="随机生成的密码", title="随机密码", default=password,
            strip=True, image=None, root=None)

猜你喜欢

转载自blog.csdn.net/Mr_Qian_Ives/article/details/107904687