51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1115|回复: 0
打印 上一主题 下一主题

[python] 随机密码生成器用Python制作

[复制链接]
  • TA的每日心情
    擦汗
    昨天 09:08
  • 签到天数: 947 天

    连续签到: 6 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2022-10-20 15:31:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    每个网站都有安全接口的某种表单,需要用户验证身份。这些表单常常使用你的电子邮件和密码来访问网站。登录时使用安全密码对于防止坏人访问你的帐户至关重要。
      本文教你如何使用Python创建一个随机密码生成器,只需生成结合字母、数字和符号的加密字符,从而使密码难以被破解或猜中。
      不妨构建一个安全的随机密码生成器。
      入手准备
      ·要构建一个随机密码生成器,我们将使用这种方法:
      · 写出所有可接受的密码字符类型,比如字母、数字和符号。
      · 让用户能够为生成的密码输入所需数量的字母、符号和数字。
      · 随机化字符顺序,使密码难以被猜中。
      创建随机密码生成器
      如你所知,互联网上的一些应用程序会在你创建新帐户时建议使用随机密码。随机字符由你决定,可以长达八个字符。
      创建一个新文件main.py,编写应用程序的脚本。
    1. <font size="3"> # main.py

    2.   letters = [

    3.       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',

    4.       'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',

    5.       'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',

    6.       'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

    7.   ]

    8.   numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    9.   symbols = ['!', '#', '[size=3]上面代码块中的字符组成了列表中显示的密码生成器的组合。[/size]
    10. [size=3]  接下来确保用户可以输入数字,这个整数代表在最终输出显示并使用变量声明时字符出现的次数。[/size]
    11. [size=3]  \n:表示输入值会进入到下面一行。[/size]
    12. [size=3]  现在,不妨更新其余代码。复制并粘贴以下内容:[/size]

    13. [code]<font size="3"> # main.py

    14.   # Password Generator Project

    15.   import random # add this

    16.   # letters, numbers, and symbols lists

    17.   # users' input for the amount of characters

    18.   # add these below

    19.   password_list = []

    20.   for char in range(1, nr_letters + 1):

    21.       password_list.append(random.choice(letters))

    22.   for char in range(1, nr_symbols + 1):

    23.       password_list.append(random.choice(numbers))

    24.   for char in range(1, nr_numbers + 1):

    25.       password_list.append(random.choice(symbols))

    26.   random.shuffle(password_list)</font>
    复制代码
    代码块执行以下操作:
      ·导入用于生成随机数的内置random模块。
      · 使用变量password_list创建空列表[]。
      · 遍历range函数中的数字,从起始索引创建数字序列,以最后一个索引加1结束。
      · 接下来,为空列表附加内容,使用random.choice()方法为每一个被声明为变量的字符获取随机选择的元素。
      · 使用.shuffle()方法对新创建的password_list进行改换,每次输入新密码就改变元素的位置。
      将密码列表转换成字符串
      复制并更新以下代码:

    1. <font size="3"> # main.py

    2.   # import

    3.   # letters, numbers, and symbols lists

    4.   # users' input for the amount of characters

    5.   # randomize characters

    6.   # add this

    7.   password = ""

    8.   for char in password_list:

    9.       password += char

    10.   # convert list to string

    11.   pwd = ''.join(password_list)

    12.   print(f"Your random password to use is: {pwd}")</font>
    复制代码
    将列表转换成字符串的过程如下:
      ·创建空字符串变量password。
      · 使用for关键字遍历密码列表。
      · 将密码字符串与循环的char变量连接起来。
      · 使用.join()方法将列表迭代由密码列表改为字符串。
      · 最后,使用f-strings显示密码的结果。
      代码最终结果:

    1. <font size="3"># main.py

    2.   import random

    3.   letters = [

    4.       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',

    5.       'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',

    6.       'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',

    7.       'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

    8.   ]

    9.   numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    10.   symbols = ['!', '#', '[b][size=3]结语[/size][/b]
    11. [size=3]  在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。[/size]

    12. [size=3][/size]


    13. , '%', '&', '(', ')', '*', '+']

    14.   print("Welcome to the PyPassword Generator!")

    15.   nr_letters = int(input("How many letters would you like in your password?\n"))

    16.   nr_symbols = int(input(f"How many symbols would you like?\n"))

    17.   nr_numbers = int(input(f"How many numbers would you like?\n"))</font>
    复制代码
    上面代码块中的字符组成了列表中显示的密码生成器的组合。
      接下来确保用户可以输入数字,这个整数代表在最终输出显示并使用变量声明时字符出现的次数。
      \n:表示输入值会进入到下面一行。
      现在,不妨更新其余代码。复制并粘贴以下内容:

    1. <font size="3"> # main.py

    2.   # Password Generator Project

    3.   import random # add this

    4.   # letters, numbers, and symbols lists

    5.   # users' input for the amount of characters

    6.   # add these below

    7.   password_list = []

    8.   for char in range(1, nr_letters + 1):

    9.       password_list.append(random.choice(letters))

    10.   for char in range(1, nr_symbols + 1):

    11.       password_list.append(random.choice(numbers))

    12.   for char in range(1, nr_numbers + 1):

    13.       password_list.append(random.choice(symbols))

    14.   random.shuffle(password_list)</font>
    复制代码
    代码块执行以下操作:
      ·导入用于生成随机数的内置random模块。
      · 使用变量password_list创建空列表[]。
      · 遍历range函数中的数字,从起始索引创建数字序列,以最后一个索引加1结束。
      · 接下来,为空列表附加内容,使用random.choice()方法为每一个被声明为变量的字符获取随机选择的元素。
      · 使用.shuffle()方法对新创建的password_list进行改换,每次输入新密码就改变元素的位置。
      将密码列表转换成字符串
      复制并更新以下代码:

    1. <font size="3"> # main.py

    2.   # import

    3.   # letters, numbers, and symbols lists

    4.   # users' input for the amount of characters

    5.   # randomize characters

    6.   # add this

    7.   password = ""

    8.   for char in password_list:

    9.       password += char

    10.   # convert list to string

    11.   pwd = ''.join(password_list)

    12.   print(f"Your random password to use is: {pwd}")</font>
    复制代码
    将列表转换成字符串的过程如下:
      ·创建空字符串变量password。
      · 使用for关键字遍历密码列表。
      · 将密码字符串与循环的char变量连接起来。
      · 使用.join()方法将列表迭代由密码列表改为字符串。
      · 最后,使用f-strings显示密码的结果。
      代码最终结果:

    1. <font size="3"># main.py

    2.   import random

    3.   letters = [

    4.       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',

    5.       'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',

    6.       'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',

    7.       'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

    8.   ]

    9.   numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

    10.   symbols = ['!', '#', '[b][size=3]结语[/size][/b]
    11. [size=3]  在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。[/size]

    12. [size=3][/size]


    13. , '%', '&', '(', ')', '*', '+']

    14.   print("Welcome to the PyPassword Generator!")

    15.   nr_letters = int(input("How many letters would you like in your password?\n"))

    16.   nr_symbols = int(input(f"How many symbols would you like?\n"))

    17.   nr_numbers = int(input(f"How many numbers would you like?\n"))

    18.   password_list = []

    19.   for char in range(1, nr_letters + 1):

    20.       password_list.append(random.choice(letters))

    21.   for char in range(1, nr_symbols + 1):

    22.       password_list.append(random.choice(numbers))

    23.   for char in range(1, nr_numbers + 1):

    24.       password_list.append(random.choice(symbols))

    25.   random.shuffle(password_list)

    26.   password = ""

    27.   for char in password_list:

    28.       password += char

    29.   print("char", char)

    30.   # convert list to string

    31.   pwd = ''.join(password_list)

    32.   print(f"Your random password to use is: {pwd}")</font>
    复制代码
    结语
      在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。




    , '%', '&', '(', ')', '*', '+']

      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"))</font>[/code]上面代码块中的字符组成了列表中显示的密码生成器的组合。
      接下来确保用户可以输入数字,这个整数代表在最终输出显示并使用变量声明时字符出现的次数。
      \n:表示输入值会进入到下面一行。
      现在,不妨更新其余代码。复制并粘贴以下内容:

    [        DISCUZ_CODE_25        ]代码块执行以下操作:
      ·导入用于生成随机数的内置random模块。
      · 使用变量password_list创建空列表[]。
      · 遍历range函数中的数字,从起始索引创建数字序列,以最后一个索引加1结束。
      · 接下来,为空列表附加内容,使用random.choice()方法为每一个被声明为变量的字符获取随机选择的元素。
      · 使用.shuffle()方法对新创建的password_list进行改换,每次输入新密码就改变元素的位置。
      将密码列表转换成字符串
      复制并更新以下代码:

    [        DISCUZ_CODE_26        ]将列表转换成字符串的过程如下:
      ·创建空字符串变量password。
      · 使用for关键字遍历密码列表。
      · 将密码字符串与循环的char变量连接起来。
      · 使用.join()方法将列表迭代由密码列表改为字符串。
      · 最后,使用f-strings显示密码的结果。
      代码最终结果:

    [        DISCUZ_CODE_27        ]结语
      在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。




    , '%', '&amp;', '(', ')', '*', '+']

      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}")</font>[/code]结语
      在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。




    , '%', '&', '(', ')', '*', '+']

      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}")</font>[/code]结语
      在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。




    , '%', '&', '(', ')', '*', '+']

      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"))</font>[/code]上面代码块中的字符组成了列表中显示的密码生成器的组合。
      接下来确保用户可以输入数字,这个整数代表在最终输出显示并使用变量声明时字符出现的次数。
      \n:表示输入值会进入到下面一行。
      现在,不妨更新其余代码。复制并粘贴以下内容:

    [        DISCUZ_CODE_25        ]代码块执行以下操作:
      ·导入用于生成随机数的内置random模块。
      · 使用变量password_list创建空列表[]。
      · 遍历range函数中的数字,从起始索引创建数字序列,以最后一个索引加1结束。
      · 接下来,为空列表附加内容,使用random.choice()方法为每一个被声明为变量的字符获取随机选择的元素。
      · 使用.shuffle()方法对新创建的password_list进行改换,每次输入新密码就改变元素的位置。
      将密码列表转换成字符串
      复制并更新以下代码:

    [        DISCUZ_CODE_26        ]将列表转换成字符串的过程如下:
      ·创建空字符串变量password。
      · 使用for关键字遍历密码列表。
      · 将密码字符串与循环的char变量连接起来。
      · 使用.join()方法将列表迭代由密码列表改为字符串。
      · 最后,使用f-strings显示密码的结果。
      代码最终结果:

    [        DISCUZ_CODE_27        ]结语
      在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。




    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-12 15:24 , Processed in 0.061797 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表