Python 使用数字、字母、符号随机生成密码

Python 使用数字、字母、符号随机生成密码,python,Python,我想生成一个随机密码 它应由符号、字母和数字组成,例如: tqpV4aJ 我!WZuYvBv7 S@OPToyu0u a) 包含6-10个字符 b) 仅包含集合{!、@、#、-、@、+、%}中的1个符号 c) 仅包含1位数字,即除数字5外的任何数字0-9,数字5不包括在内 d) 在剩余字符中包含大写或小写字母,但“e”和“e”除外 e) 字符的位置是随机选择的 我怎样才能以蟒蛇式的方式实现这一点 我的尝试(失败:只能输出一个字母): 应该从评论中相当清楚。基本思想是: 建立可供选择的列表 作

我想生成一个随机密码

它应由符号、字母和数字组成,例如:

tqpV4aJ

我!WZuYvBv7

S@OPToyu0u

a) 包含6-10个字符

b) 仅包含集合{!、@、#、-、@、+、%}中的1个符号

c) 仅包含1位数字,即除数字5外的任何数字0-9,数字5不包括在内

d) 在剩余字符中包含大写或小写字母,但“e”和“e”除外

e) 字符的位置是随机选择的

我怎样才能以蟒蛇式的方式实现这一点


我的尝试(失败:只能输出一个字母):


应该从评论中相当清楚。基本思想是:

  • 建立可供选择的列表
  • 作出选择
  • 加入单个列表
  • 洗牌
  • 重新连接到单个字符串
应注意,这是不安全的,不应用于实际生成密码

随机导入
导入字符串
#把你的常数放在上面,用大写字母写,这很好
符号=(“!”、“@”、“#”、“-”、“@”、“+”、“%”)
#从数字列表中省略“5”很容易
#让列表已经是字符串比乱用int更容易
数字=('0','1','2','3','4','6','7','8','9')
#省略“e”和“e”的快速列表理解
字母=[char for char in string.ascii_如果char!=“e”和char!=“e”]
#将所有逻辑放在一个函数中
def create_password_unsecure():
#选择一个符号
所选符号=随机。选择(符号)
#选一个数字
所选数字=随机。选择(数字)
#挑选一些字母来填写我们的6-10个字符
#我们选择4-8而不是6-10,因为2个字符将是符号/数字
字母数=random.randint(4,8)
#快速列表选择我们的信件
所选字母=[随机选择(字母)范围内的i(字母数)]
#在一个列表中获取所有元素
密码\u元素\u列表=[选定的\u符号]+[选定的\u编号]+选定的\u字母
#(伪)将列表随机化
随机.shuffle(密码\u元素\u列表)
伪随机密码=''.join(密码元素列表)
打印(伪随机密码)
创建\u密码\u不安全()

从评论中应该非常清楚。基本思想是:

  • 建立可供选择的列表
  • 作出选择
  • 加入单个列表
  • 洗牌
  • 重新连接到单个字符串
应注意,这是不安全的,不应用于实际生成密码

随机导入
导入字符串
#把你的常数放在上面,用大写字母写,这很好
符号=(“!”、“@”、“#”、“-”、“@”、“+”、“%”)
#从数字列表中省略“5”很容易
#让列表已经是字符串比乱用int更容易
数字=('0','1','2','3','4','6','7','8','9')
#省略“e”和“e”的快速列表理解
字母=[char for char in string.ascii_如果char!=“e”和char!=“e”]
#将所有逻辑放在一个函数中
def create_password_unsecure():
#选择一个符号
所选符号=随机。选择(符号)
#选一个数字
所选数字=随机。选择(数字)
#挑选一些字母来填写我们的6-10个字符
#我们选择4-8而不是6-10,因为2个字符将是符号/数字
字母数=random.randint(4,8)
#快速列表选择我们的信件
所选字母=[随机选择(字母)范围内的i(字母数)]
#在一个列表中获取所有元素
密码\u元素\u列表=[选定的\u符号]+[选定的\u编号]+选定的\u字母
#(伪)将列表随机化
随机.shuffle(密码\u元素\u列表)
伪随机密码=''.join(密码元素列表)
打印(伪随机密码)
创建\u密码\u不安全()

我相信您的方法可以通过更多的列表和仔细的字符替换来简化:

import random
import string

def get_password():
    pass_ = [random.choice(list(set(string.ascii_letters) - {"e", "E"})) for i in range(random.randint(6, 10))]  # Initial password contains random letters, excluding e and E
    index_1 = random.randint(0, len(pass_) - 1)
    pass_[index_1] = random.choice(["!", "@", "#", "-", "@", "+", "%"])  # Replace a random character with a symbol
    index_2 = index_1  # Initialize index_2 as equal to index_1 so that it will be randomized at least once

    while index_1 == index_2:  # Ensure the two indexes are not equal
        index_2 = random.randint(0, len(pass_) - 1)

    pass_[index_2] = str(random.choice([random.randint(0, 4), random.randint(6, 9)]))  # Replace the chosen random character with a random digit, excluding 5

    return "".join(pass_)

print(get_password())

我相信您的方法可以通过更多列表和仔细的字符替换来简化:

import random
import string

def get_password():
    pass_ = [random.choice(list(set(string.ascii_letters) - {"e", "E"})) for i in range(random.randint(6, 10))]  # Initial password contains random letters, excluding e and E
    index_1 = random.randint(0, len(pass_) - 1)
    pass_[index_1] = random.choice(["!", "@", "#", "-", "@", "+", "%"])  # Replace a random character with a symbol
    index_2 = index_1  # Initialize index_2 as equal to index_1 so that it will be randomized at least once

    while index_1 == index_2:  # Ensure the two indexes are not equal
        index_2 = random.randint(0, len(pass_) - 1)

    pass_[index_2] = str(random.choice([random.randint(0, 4), random.randint(6, 9)]))  # Replace the chosen random character with a random digit, excluding 5

    return "".join(pass_)

print(get_password())

听起来你想要:

import random, string

symbols = '!@#-@+%'   #choose 1
digits  = '123467890' #choose 1
letters = set(string.ascii_letters)-set('eE') #choose 4 to 8

chars   = random.sample(letters,random.randint(4,8))
chars.append(random.choice(symbols))
chars.append(random.choice(digits))
random.shuffle(chars)

pwd = ''.join(chars)

听起来你想要:

import random, string

symbols = '!@#-@+%'   #choose 1
digits  = '123467890' #choose 1
letters = set(string.ascii_letters)-set('eE') #choose 4 to 8

chars   = random.sample(letters,random.randint(4,8))
chars.append(random.choice(symbols))
chars.append(random.choice(digits))
random.shuffle(chars)

pwd = ''.join(chars)

这里写的所有答案都应该使用而不是
随机
模块来生成机密值,包括密码。特别是:

  • random.randint(0,n)
    应替换为
    secrets.randown(n+1)
  • random.choice(x)
    应替换为
    secrets.choice(x)
  • random.choices(…)
    应替换为
    secrets.SystemRandom().choices(…)
    random.SystemRandom().choices(…)
  • random.shuffle(…)
    应替换为
    secrets.SystemRandom().shuffle(…)
    random.SystemRandom().shuffle(…)

random
模块中的函数(包括
random.randint
random.choice
random.choices
random.sample
random.shuffle
)使用全局随机数生成器,该生成器不一定是为信息安全而设计的,因此,使用这些函数可以使密码比现在更容易猜测——特别是考虑到问题中给出的最大长度要求和受限字符分布。一般来说,每当调用
random.*
函数生成密码时,它们都应该被
secrets.SystemRandom().*
替换(或者是
secrets
模块中的合适方法)。

这里写的所有答案都应该使用
random
模块生成秘密值,包括密码。特别是:

  • random.randint(0,n)
    应替换为
    secrets.randown(n+1)
  • random.choice(x)
    应替换为
    secrets.choice(x)
  • random.cho