我刚开始学习Python,并为我们的Service Desk(一个密码生成器)启动了一个小项目。它可以工作,但是我希望它能够获取用户输入并创建另一个密码。我已经做了很多搜索并找到了大量的密码生成器,我已经弄清楚如何重复一个密码,但不知道如何在输入上生成新的passowrd。我的代码如下,将在输入时重复输入密码。任何帮助将不胜感激。
import random
alphabet = "abcdefghijklmnopqrstuvwxyz"
pw_length = 6
mypw = ""
for i in range(pw_length):
next_index = random.randrange(len(alphabet))
mypw = mypw + alphabet[next_index]
# replace 1 or 2 characters with a number
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(mypw)//2)
mypw = mypw[0:replace_index] + str(random.randrange(10)) +
mypw[replace_index+1:]
# replace 1 or 2 letters with an uppercase letter
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(mypw)//2,len(mypw))
mypw = mypw[0:replace_index] + mypw[replace_index].upper() +
mypw[replace_index+1:]
print("Your password is" + (mypw))
inp = input()
while inp != "":
print(mypw)
inp = input()
答案 0 :(得分:0)
你必须通过将所有内容放入while循环来重复密码生成,或者更好,编写一个函数来生成一个密码并在while循环中调用此函数:
import random
alphabet = "abcdefghijklmnopqrstuvwxyz"
pw_length = 6
def generate_password(pw_length):
mypw = ""
for i in range(pw_length):
next_index = random.randrange(len(alphabet))
mypw = mypw + alphabet[next_index]
# replace 1 or 2 characters with a number
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(mypw)//2)
mypw = mypw[0:replace_index] + str(random.randrange(10)) +
mypw[replace_index+1:]
# replace 1 or 2 letters with an uppercase letter
for i in range(random.randrange(1,3)):
replace_index = random.randrange(len(mypw)//2,len(mypw))
mypw = mypw[0:replace_index] + mypw[replace_index].upper() +
mypw[replace_index+1:]
return mypw
while True:
print("Your password is", generate_password(pw_length))
inp = input()
if not inp:
break
或略微优化的版本:
import string
import random
def generate_password(pw_length):
charsets = []
# take 1 or 2 numbers
for i in range(random.randrange(1,3)):
charsets.append(string.digits)
# take 1 or 2 uppercase
for i in range(random.randrange(1,3)):
charsets.append(string.ascii_uppercase)
# fill up with lowercase up to pw_length
while len(charsets) < pw_length:
charsets.append(string.ascii_lowercase)
# generate password of charsets
random.shuffle(charsets)
return ''.join(random.choice(cs) for cs in charsets)
while True:
print("Your password is", generate_password(pw_length))
inp = input()
if not inp:
break
答案 1 :(得分:0)
不幸的是无法评论我的评分,但我猜哈希输入会有效吗?因为它产生一个随机的&#39;串。 另外,散列是一种存储密码的好方法。
def Generate_Password():
input_ = input('please enter your password: ')
#i saw you have a limit of length 6, but you can tweak it
if len(input_) > 6:
print ('Max length for a pssword is 6')
hashPassword = hashlib.sha3_256(b'{input_}').hexdigest()
return (hashPassword)
答案 2 :(得分:0)
生成密码 密码需要全部字符组合
类密码:
def init (自己):
#self.choice =选择
self.menu = {1:“生成通行证”,3:“退出”}
def main(self):
对于self.menu.items()中的x,y:
打印x,y
#print'\ n'
#print'选择一个\ n'
打印'\ n'
尝试:
self.choice = int(raw_input(“输入选择:”))
除了:
print(“没有有效的整数!请尝试...”)
self.main()
self.check()
def check(self):
#print 'select one\n'
if self.choice == 1:
self.genpass()
elif self.choice == 3:
self.exi(exit)
else:
print 'invalid entry try again'
self.main()
#print 'invalid'
def genpass(self):
import random
import re
import string
min_len = 8
max_len = 12
self.passlen = random.randint(min_len, max_len)
#s = "@4A%1aXb6!B2#8lZ$t5&"
#random.shuffle(s)
#password = "".join(random.sample(s,passlen))
#print 'Generate password \n', password, '\n'
self.password1 = ""
SpecialChars = "!@#$%%^*"
self.password1 += random.choice(string.ascii_uppercase)
self.password1 += random.choice(string.ascii_lowercase)
self.password1 += random.choice(string.digits)
self.password1 += random.choice(SpecialChars)
for i in range(0,self.passlen):
self.password1 += random.choice(string.ascii_uppercase +
string.ascii_lowercase +
string.digits+
SpecialChars)
self.all_symbol = ''.join(random.sample(self.password1,self.passlen))
flag =0
while True:
if not re.search("[a-z]", self.all_symbol):
flag = -1
break
elif not re.search("[A-Z]", self.all_symbol):
flag = -1
break
elif not re.search("[0-9]", self.all_symbol):
flag = -1
break
elif not re.search("[_@$]", self.all_symbol):
flag = -1
break
elif re.search("\s", self.all_symbol):
flag = -1
break
else:
flag = 0
print 'password Length\t', self.passlen
print 'Generate password is\t', self.all_symbol, '\n'
break
if flag ==-1:
self.genpass()
#print len(password)
def exi(self, exit):
import sys
sys.exit()
为True时: p =密码() p.main()