新聞中心
作者 | Teri Eyenike

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到溆浦網(wǎng)站設(shè)計(jì)與溆浦網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋溆浦地區(qū)。
譯者 | 布加迪
審校丨諾亞
每個(gè)網(wǎng)站都有安全接口的某種表單,需要用戶驗(yàn)證身份。這些表單常常使用你的電子郵件和密碼來(lái)訪問(wèn)網(wǎng)站。登錄時(shí)使用安全密碼對(duì)于防止壞人訪問(wèn)你的帳戶至關(guān)重要。
本文教你如何使用Python創(chuàng)建一個(gè)隨機(jī)密碼生成器,只需生成結(jié)合字母、數(shù)字和符號(hào)的加密字符,從而使密碼難以被破解或猜中。
不妨構(gòu)建一個(gè)安全的隨機(jī)密碼生成器。
入手準(zhǔn)備
要構(gòu)建一個(gè)隨機(jī)密碼生成器,我們將使用這種方法:
寫出所有可接受的密碼字符類型,比如字母、數(shù)字和符號(hào)。
讓用戶能夠?yàn)樯傻拿艽a輸入所需數(shù)量的字母、符號(hào)和數(shù)字。
隨機(jī)化字符順序,使密碼難以被猜中。
創(chuàng)建隨機(jī)密碼生成器
如你所知,互聯(lián)網(wǎng)上的一些應(yīng)用程序會(huì)在你創(chuàng)建新帳戶時(shí)建議使用隨機(jī)密碼。隨機(jī)字符由你決定,可以長(zhǎng)達(dá)八個(gè)字符。
??創(chuàng)建一個(gè)新文件main.py??,編寫應(yīng)用程序的腳本。
# main.py
letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
上面代碼塊中的字符組成了列表中顯示的密碼生成器的組合。
接下來(lái)確保用戶可以輸入數(shù)字,這個(gè)整數(shù)代表在最終輸出顯示并使用變量聲明時(shí)字符出現(xiàn)的次數(shù)。
\n:表示輸入值會(huì)進(jìn)入到下面一行
現(xiàn)在,不妨更新其余代碼。復(fù)制并粘貼以下內(nèi)容:
# main.py
# Password Generator Project
import random # add this
# letters, numbers, and symbols lists
# users' input for the amount of characters
# add these below
password_list = []
for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))
for char in range(1, nr_symbols + 1):
password_list.append(random.choice(numbers))
for char in range(1, nr_numbers + 1):
password_list.append(random.choice(symbols))
random.shuffle(password_list)
代碼塊執(zhí)行以下操作:
- 導(dǎo)入用于生成隨機(jī)數(shù)的內(nèi)置random模塊。
- 使用變量password_list創(chuàng)建空列表[]。
- 遍歷range函數(shù)中的數(shù)字,從起始索引創(chuàng)建數(shù)字序列,以最后一個(gè)索引加1結(jié)束。
- 接下來(lái),為空列表附加內(nèi)容,使用random.choice()方法為每一個(gè)被聲明為變量的字符獲取隨機(jī)選擇的元素。
- 使用.shuffle()方法對(duì)新創(chuàng)建的password_list進(jìn)行改換,每次輸入新密碼就改變?cè)氐奈恢谩?/li>
將密碼列表轉(zhuǎn)換成字符串
復(fù)制并更新以下代碼:
# main.py
# import
# letters, numbers, and symbols lists
# users' input for the amount of characters
# randomize characters
# add this
password = ""
for char in password_list:
password += char
# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")
將列表轉(zhuǎn)換成字符串的過(guò)程如下:
- 創(chuàng)建空字符串變量password。
- 使用for關(guān)鍵字遍歷密碼列表。
- 將密碼字符串與循環(huán)的char變量連接起來(lái)。
- 使用.join()方法將列表迭代由密碼列表改為字符串。
- 最后,使用f-strings顯示密碼的結(jié)果。
代碼最終結(jié)果:
# main.py
import random
letters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password_list = []
for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))
for char in range(1, nr_symbols + 1):
password_list.append(random.choice(numbers))
for char in range(1, nr_numbers + 1):
password_list.append(random.choice(symbols))
random.shuffle(password_list)
password = ""
for char in password_list:
password += char
print("char", char)
# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")
結(jié)語(yǔ)
在本文中你開發(fā)了一個(gè)應(yīng)用程序,該應(yīng)用程序每次生成不同的隨機(jī)密碼,從而支持動(dòng)態(tài)使用環(huán)境,生成盡可能多的密碼。
原文鏈接:
??https://hackernoon.com/how-to-create-a-random-password-generator-using-python??
當(dāng)前文章:如何使用Python制作隨機(jī)密碼生成器
URL標(biāo)題:http://m.fisionsoft.com.cn/article/dhioepo.html


咨詢
建站咨詢
