1. 程式人生 > 程式設計 >python如何隨機生成高強度密碼

python如何隨機生成高強度密碼

本文例項為大家分享了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,encoding='utf8') as file:
 file.writelines("賬戶ID:" + accountID + '\n')
 file.writelines('密碼:' + password)
 file.close()
# 展示密碼
print('生成的密碼為:' + password)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。