Python創(chuàng)建高強度密碼生成工具方法實例
規(guī)劃項目
學習編程最好的辦法就是實際寫一個項目,我們這里通過 Python 來編寫密碼生成器。
在互聯網時代,賬戶就像空氣一樣常見,創(chuàng)建強大而安全的密碼是一項任重道遠的任務。雖然密碼生成工具數不勝數,但是你有沒有想過通過自己的雙手來創(chuàng)建一個密碼生成工具,讓我們來構建一個生成密碼的項目吧。
首先,在構建之前、我們需要知道我們想從這個項目中得到什么,這個工具可以生成隨機數字、符號和字母組合的密碼。
我們在構建項目時可以學到以下內容:
• 變量和數據類型
• 用戶輸入和輸出
• 條件語句(if-else)
• 隨機數生成
• 函數
我們了解我們的目標之后,繼續(xù)說明這個密碼工具的流程:
• 獲取密碼長度,在這里我把 12 作為默認長度
• 創(chuàng)建字符池(小寫、大寫、符號、數字)
• 生成密碼
創(chuàng)建項目
1. 導入庫
首先要導入兩個必要的庫:string
和 random
。
string
庫提供各種與字符串相關的函數和常量。它包含 ascii_lowercase
、ascii_uppercase
、標點符號和數字常量,分別代表相應的字符集。
random
庫提供生成隨機數和序列的函數。它包含 choice()
函數,可從給定序列中隨機選擇一個元素。
import string import random
2. 定義 generate_password() 函數
generate_password()
函數是密碼生成器的核心。它接受一個指定所需密碼長度的可選參數 length
。
這個函數包含一個默認參數 length
,密碼字符長度,如果不提供這個參數,那么就默認使用 12 的長度。
字符池(character pool
)的生成來自 string
這個庫,包含了小寫字母 ascii_lowercase
,大寫字母 ascii_uppercase
、標點符號 punctuation
和數字 digits
。
然后從字符池中隨機添加一個字符到 password
字符串中??梢岳斫鉃椴粩嗬奂拥?nbsp;length
限制的長度。
def generate_password(length=12): # 定義 character pool character_pool = string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.digits password = "" for _ in range(length): password += random.choice(character_pool) return password
3. 生成并展示密碼
在函數外部,代碼調用 generate_password()
函數生成隨機密碼。它將生成的密碼賦值給 generated_password
變量,并使用 print()
將其顯示給用戶。
generated_password = generate_password() print("你的密碼是:", generated_password)
不如我們?yōu)槊艽a生成器增加一些功能:
• 可以輸入密碼的長度
• 可以選擇是否使用符號、數字和大寫字母。
好了,讓我們在項目中添加新代碼。
4. 定義字符集
lowercase_letters = string.ascii_lowercase uppercase_letters = string.ascii_uppercase symbols = string.punctuation numbers = string.digits
這幾行代碼定義了四個變量:小寫字母、大寫字母、符號和數字。每個變量都包含一個字符串,其中包含相應的字符集。string
模塊提供了這些常量,以便于訪問不同的字符組。
5. 根據用戶指定的標準創(chuàng)建密碼
character_pool = lowercase_letters if include_symbols: character_pool += symbols if include_numbers: character_pool += numbers if include_uppercase: character_pool += uppercase_letters
這些 if
語句會檢查用戶輸入的每種字符類型(符號、數字、大寫字母)。如果用戶表示要包含特定的字符類型,相應的字符集就會附加到 character_pool
字符串中。這樣可以確保密碼生成器在生成密碼時添加指定的字符類型。
6. 生成密碼
for _ in range(length): password += random.choice(character_pool) return password
此循環(huán)迭代 length
次,其中 length
為指定的密碼長度。在每次迭代中,使用 random.choice()
函數從字符庫中隨機選擇一個字符。最后將返回密碼字符串。
7. 獲取用戶輸入的密碼標準
password_length = int(input("請輸入密碼長度:")) include_symbols = input("包含標點符號 (y/n): ") == "y" include_numbers = input("包含數字 (y/n): ") == "y" include_uppercase = input("包含大寫字母 (y/n): ") == "y"
這幾行代碼用來收集用戶對密碼標準(長度、符號、數字和大寫字母)的要求,并將其存儲在相應的變量中。
運行代碼
基礎代碼:
import string import random def generate_password(length=12): # 定義 character pool character_pool = string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.digits password = "" for _ in range(length): password += random.choice(character_pool) return password # 生成并展示密碼 generated_password = generate_password() print("你生成的密碼是:", generated_password) # 運行結果 # 你生成的密碼是:\{Ckz<@svHN!
完善后的代碼
允許用戶指定生成高強度的安全密碼:
import string import random def generate_password(length=12, include_symbols=True, include_numbers=True, include_uppercase=True): password = "" # 定義字符集 lowercase_letters = string.ascii_lowercase uppercase_letters = string.ascii_uppercase symbols = string.punctuation numbers = string.digits # 基于用戶的要求創(chuàng)建字符池 character_pool = lowercase_letters if include_symbols: character_pool += symbols if include_numbers: character_pool += numbers if include_uppercase: character_pool += uppercase_letters # 生成密碼 for _ in range(length): password += random.choice(character_pool) return password # 根據用戶輸入來確定密碼要求 password_length = int(input("請輸入密碼長度:")) include_symbols = input("包含標點符號 (y/n):") == "y" include_numbers = input("包含數字 (y/n):") == "y" include_uppercase = input("包含大寫字母 (y/n):") == "y" # 生成并展示密碼 generated_password = generate_password(password_length, include_symbols, include_numbers, include_uppercase) print("你生成的密碼是:", generated_password)
運行結果
請輸入密碼長度:6
包含標點符號(y/n):y
包含數字 (y/n):y
包含大寫字母 (y/n):y
你生成的密碼是:z-=Sx<
這份代碼所構建的密碼生成器是一款簡單有效的密碼生成工具。它允許用戶自定義密碼長度和字符類型,用途廣泛,可滿足個人需求。
以上就是Python創(chuàng)建高強度密碼生成工具方法實例的詳細內容,更多關于Python創(chuàng)建密碼生成工具的資料請關注腳本之家其它相關文章!
相關文章
Python實例方法、類方法、靜態(tài)方法的區(qū)別與作用詳解
這篇文章主要介紹了Python實例方法、類方法、靜態(tài)方法的區(qū)別與作用,結合實例形式分析了Python面向對象程序設計中實例方法、類方法、靜態(tài)方法的概念、原理、用法及相關操作技巧,需要的朋友可以參考下2019-03-03