Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python生成8个字符的密码,包括大小写和数字_Python_Password Generator - Fatal编程技术网

使用python生成8个字符的密码,包括大小写和数字

使用python生成8个字符的密码,包括大小写和数字,python,password-generator,Python,Password Generator,我想使用python生成密码,包括小写、大写和数字,但它应该保证所有这3种密码都已使用。到目前为止,我写了这篇文章,但它不能保证所有3种字符都被使用。我想把8个字符分成2部分。前三名和后五名。然后确保在第一部分中所有3种角色都被使用,然后在下一部分中对它们进行洗牌,但我不知道如何编码 import random a = 0 password = '' while a < 8: if random.randint(0, 61) < 10: password +=

我想使用python生成密码,包括小写、大写和数字,但它应该保证所有这3种密码都已使用。到目前为止,我写了这篇文章,但它不能保证所有3种字符都被使用。我想把8个字符分成2部分。前三名和后五名。然后确保在第一部分中所有3种角色都被使用,然后在下一部分中对它们进行洗牌,但我不知道如何编码

import random
a = 0
password = ''
while a < 8:
    if random.randint(0, 61) < 10:
        password += chr(random.randint(48, 57))
    elif 10<random.randint(0, 61)<36:
        password += chr(random.randint(65, 90))
    else:
        password += chr(random.randint(97, 122))
    a += 1
print(password)
随机导入
a=0
密码=“”
而a<8:
如果random.randint(0,61)<10:
密码+=chr(random.randint(48,57))

elif 10你的问题由三部分组成:

  • 将8个字符分成2部分-前3部分和后5部分-(字符串切片)
  • 确保在第一部分中使用了所有3种字符(验证密码)
  • 洗牌字符(洗牌字符串)

  • 第1部分:切片字符串

    if password_check(password) == True:
        new_pwd = ''.join(random.sample(password,len(password)))
        print new_pwd
    
    这里有一个很好的教程,教你如何

    在您的情况下,如果在代码末尾插入此项

    print(password[:3])
    print(password[3:])
    
    。。。您将看到前3个字符和最后5个字符


    第2部分:验证密码

    可以找到一个很好的答案

    此代码将洗牌整个密码,并将其分配给名为
    new\u pwd


    附言。
    整个代码都可以找到

    正如您所说:前三个位置分别从大写和小写字符和数字中随机选择,其余位置从所有字符中随机选择,然后随机移动

    from random import choice, shuffle
    from string import ascii_uppercase, ascii_lowercase, digits
    pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
          + [choice(ascii_lowercase + ascii_uppercase + digits) for _ in range(5)]
    shuffle(pwd)
    pwd = ''.join(pwd)
    

    请注意,这样数字可能有点“代表性不足”,因为它们是从所有可用字符中随机选择的,因此前三个字符之后的任何字符都有可能是数字。如果希望1/3的字符为数字(这可能不是一个好主意,请参见下文),可以首先随机选择一个字符组,每个字符组有1/3的机会,然后从该组中选择一个字符:

    pwd = [choice(ascii_lowercase), choice(ascii_uppercase), choice(digits)] \
          + [choice(choice([ascii_lowercase, ascii_uppercase, digits])) for _ in range(5)]
    

    但请注意,这将减少随机性,从而降低密码的安全性——但三个组中至少各有一个组的要求也是如此。

    您可以使用正则表达式并检查是否有字符与正则表达式匹配

    import re
    string = "AaBbCc12"
    
    if re.search('[a-z]',string)\
    and re.search('[0-9]',string)\ 
    and re.search('[A-Z]',string):
        print("This is a string you are looking for")
    else:
        print("Nope")
    

    这是一个相当不雅观的解决方案,但在理解和适应性方面是最快的。

    我认为在编辑之前最好。。。从安全角度来看,更改字符的相对概率肯定是不好的。@glibdud同意。添加了一个通知。更好。我仍然看不出那部分的价值,尽管。。。前半部分完全解决了这个问题。(+1,尽管如此)
    import re
    string = "AaBbCc12"
    
    if re.search('[a-z]',string)\
    and re.search('[0-9]',string)\ 
    and re.search('[A-Z]',string):
        print("This is a string you are looking for")
    else:
        print("Nope")