如何使用 Python 創建密碼生成器?
已發表: 2021-08-20安全是我們生活中最重要的部分之一。 隨著大多數事情都在網上進行,安全性的重要性日益增加。 當我們談論安全性時,密碼就會浮出水面。
在本文中,我們將創建一個密碼生成器,幫助我們快速生成隨機和強密碼。
為什麼我們需要密碼生成器?
因為我們無法立即想到不同的密碼模式。
但是,計算機的情況並非如此。 計算機可以在幾秒鐘內根據我們的自定義生成隨機和強密碼。 有許多可用的密碼生成器。
我們可以用我們喜歡的自定義來創建我們自己的嗎?
是的,我們絕對可以創造一個。 在這裡,我們將向您展示如何做到這一點。
讓我們創建一個密碼生成器。
密碼生成器
創建我們自己的密碼生成器的最大好處是我們可以隨心所欲地對其進行自定義。
首先,我們將創建一個密碼生成器,詢問密碼的長度並生成一個包含數字、字母和特殊字符的隨機密碼。
接下來,我們將通過詢問每種類型字符的數量來改進它,例如數字、字母和特殊字符的數量。
因此,事不宜遲,讓我們看看使用 Python 創建密碼生成器的步驟。
腳步
- 將所有字符存儲為列表。 我們可以使用 Python 的 string 模塊,也可以全部輸入。
- 要求用戶輸入密碼的長度。
- 使用
random.shuffle方法隨機播放字符。 - 初始化一個空列表來存儲密碼。
- 編寫一個迭代
length時間的循環。- 使用
random.choice方法從所有字符中隨機選擇一個字符。 - 將隨機字符附加到密碼中。
- 使用
- 隨機排列生成的密碼列表,使其更加隨機。
- 使用
join方法將密碼列表轉換為字符串。 - 打印密碼。
按照上面的步驟,嘗試寫代碼。 不用擔心,即使您不會編寫代碼。 查看下面的代碼。
代碼
import string import random ## characters to generate password from characters = list(string.ascii_letters + string.digits + "[email protected]#$%^&*()") def generate_random_password(): ## length of password from the user length = int(input("Enter password length: ")) ## shuffling the characters random.shuffle(characters) ## picking random characters from the list password = [] for i in range(length): password.append(random.choice(characters)) ## shuffling the resultant password random.shuffle(password) ## converting the list to string ## printing the list print("".join(password)) ## invoking the function generate_random_password()上面的代碼是不言自明的。 我們剛剛按照描述的步驟編寫代碼。 因此,如果您閱讀這些步驟,您在理解代碼方面不會發現任何問題。
現在,運行代碼並生成密碼。 示例輸出如下。
Enter password length: 10 d&amIzK%d)觀察上面輸出中的密碼。 有數字嗎? 不可以。因為不能保證該程序將包含數字。
接下來,我們嘗試通過要求用戶輸入他們想要的數字、字母和特殊字符的數量來保證程序將包含數字和特殊字符。
當用戶輸入每種類型的字符數時,程序會將相應的字符類型數包含在密碼中。
讓我們看看接受每種類型的字符數並生成密碼的代碼。
import string import random ## characters to generate password from alphabets = list(string.ascii_letters) digits = list(string.digits) special_characters = list("[email protected]#$%^&*()") characters = list(string.ascii_letters + string.digits + "[email protected]#$%^&*()") def generate_random_password(): ## length of password from the user length = int(input("Enter password length: ")) ## number of character types alphabets_count = int(input("Enter alphabets count in password: ")) digits_count = int(input("Enter digits count in password: ")) special_characters_count = int(input("Enter special characters count in password: ")) characters_count = alphabets_count + digits_count + special_characters_count ## check the total length with characters sum count ## print not valid if the sum is greater than length if characters_count > length: print("Characters total count is greater than the password length") return ## initializing the password password = [] ## picking random alphabets for i in range(alphabets_count): password.append(random.choice(alphabets)) ## picking random digits for i in range(digits_count): password.append(random.choice(digits)) ## picking random alphabets for i in range(special_characters_count): password.append(random.choice(special_characters)) ## if the total characters count is less than the password length ## add random characters to make it equal to the length if characters_count < length: random.shuffle(characters) for i in range(length - characters_count): password.append(random.choice(characters)) ## shuffling the resultant password random.shuffle(password) ## converting the list to string ## printing the list print("".join(password)) ## invoking the function generate_random_password()那麼,前面的代碼和這段代碼有什麼區別呢?

- 我們為每種類型的字符編寫了單獨的循環以將它們添加到密碼中。
- 有兩個條件檢查來驗證總字符數和密碼長度。
我們添加了一些額外的代碼。 但是,該模式與之前的代碼類似。 所以,你不會覺得理解它有任何困難。
現在,是時候執行並檢查輸出了。 看下面的測試運行。
Enter password length: 10 Enter alphabets count in password: 3 Enter digits count in password: 2 Enter special characters count in password: 3 V2(&#XlQq1如果您看到這次生成的密碼,則它具有用戶想要的最少字符數。 並且該程序還包含了 2 個隨機字符,使密碼長度等於用戶輸入。
歡呼! 我們有一個完整的強密碼生成器。
結論
我們已經看到瞭如何從頭開始創建密碼生成器。 我們可以為其添加更多功能嗎? 是的,我們可以添加任意數量的。 但是,請確保生成的密碼足夠強。
從代碼生成密碼並將其用於您的下一個在線帳戶。
接下來,了解如何在 Python 中構建井字遊戲。
快樂編碼!
