如何使用Python制作随机密码生成器

发表于:2022-10-20 09:43

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:布加迪    来源:51CTO技术栈

  每个网站都有安全接口的某种表单,需要用户验证身份。这些表单常常使用你的电子邮件和密码来访问网站。登录时使用安全密码对于防止坏人访问你的帐户至关重要。
  本文教你如何使用Python创建一个随机密码生成器,只需生成结合字母、数字和符号的加密字符,从而使密码难以被破解或猜中。
  不妨构建一个安全的随机密码生成器。
  入手准备
  ·要构建一个随机密码生成器,我们将使用这种方法:
  · 写出所有可接受的密码字符类型,比如字母、数字和符号。
  · 让用户能够为生成的密码输入所需数量的字母、符号和数字。
  · 随机化字符顺序,使密码难以被猜中。
  创建随机密码生成器
  如你所知,互联网上的一些应用程序会在你创建新帐户时建议使用随机密码。随机字符由你决定,可以长达八个字符。
  创建一个新文件main.py,编写应用程序的脚本。
  # 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"))
  上面代码块中的字符组成了列表中显示的密码生成器的组合。
  接下来确保用户可以输入数字,这个整数代表在最终输出显示并使用变量声明时字符出现的次数。
  \n:表示输入值会进入到下面一行。
  现在,不妨更新其余代码。复制并粘贴以下内容:
  # 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)
  代码块执行以下操作:
  ·导入用于生成随机数的内置random模块。
  · 使用变量password_list创建空列表[]。
  · 遍历range函数中的数字,从起始索引创建数字序列,以最后一个索引加1结束。
  · 接下来,为空列表附加内容,使用random.choice()方法为每一个被声明为变量的字符获取随机选择的元素。
  · 使用.shuffle()方法对新创建的password_list进行改换,每次输入新密码就改变元素的位置。
  将密码列表转换成字符串
  复制并更新以下代码:
  # 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}")
  将列表转换成字符串的过程如下:
  ·创建空字符串变量password。
  · 使用for关键字遍历密码列表。
  · 将密码字符串与循环的char变量连接起来。
  · 使用.join()方法将列表迭代由密码列表改为字符串。
  · 最后,使用f-strings显示密码的结果。
  代码最终结果:
  # 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}")
  结语
  在本文中你开发了一个应用程序,该应用程序每次生成不同的随机密码,从而支持动态使用环境,生成尽可能多的密码。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号