当前位置: 移动技术网 > IT编程>脚本编程>Python > python如何随机生成高强度密码

python如何随机生成高强度密码

2020年08月20日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了python随机生成高强度密码的具体代码,供大家参考,具体内容如下import randomimport re# 字母类型englishchar = ['q', 'w', 'e',

本文实例为大家分享了python随机生成高强度密码的具体代码,供大家参考,具体内容如下

import random
import re

# 字母类型
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 = []
# 选择密码类型
print('1、字母')
print('2、字母+数字')
print('3、字母+数字+符号')
typepassword = input('输入你的密码类型选择(数字):')
# 判断输入是否合法
if not re.fullmatch('[1-3]', typepassword):
 print("\033[37;41m 不要跟我皮\033[0m")
 exit(0)
# 初始化密码类型
if typepassword.__eq__('1'):
 allchar = englishchar.copy()
if typepassword.__eq__('2'):
 allchar = englishchar.copy() + numberchar.copy()
if typepassword.__eq__('3'):
 allchar = englishchar.copy() + numberchar.copy() + symbolchar.copy()
# 重新洗牌数组
random.shuffle(allchar)
# 配置基本信息
account = input('你为哪个账号设置密码?:')
accountid = input('输入账户id:')
passwordlength = input('密码长度是多少(25>p>7):')
# 检测用户输入是否合法
if not passwordlength.isdigit() and 25 > int(passwordlength) > 7:
 print("\033[37;41m 不要跟我皮\033[0m")
 exit(0)
# 循环生成密码
for i in range(int(passwordlength)):
 a = len(allchar) - 1
 password = password + allchar[random.randint(0, a)]

# 密码文件备份
with open('/users/apple/专业知识/密码/' + account, 'w', encoding='utf8') as file:
 file.writelines("账户id:" + accountid + '\n')
 file.writelines('密码:' + password)
 file.close()
# 展示密码
print('生成的密码为:' + password)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网