鱼C论坛

 找回密码
 立即注册
查看: 2039|回复: 3

关于密码生成的问题

[复制链接]
发表于 2021-7-5 18:56:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
大佬们,我在做作业的时候遇到了个问题
这是代码
  1. # 密码安全性检查代码
  2. #
  3. # 低级密码要求:
  4. #   1. 密码由单纯的数字或字母组成
  5. #   2. 密码长度小于等于8位
  6. #
  7. # 中级密码要求:
  8. #   1. 密码必须由数字、字母或特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)任意两种组合
  9. #   2. 密码长度不能低于8位
  10. #
  11. # 高级密码要求:
  12. #   1. 密码必须由数字、字母及特殊字符(仅限:~!@#$%^&*()_=-/,.?<>;:[]{}|\)三种组合
  13. #   2. 密码只能由字母开头
  14. #   3. 密码长度不能低于16位
  15. import string

  16. word = string.ascii_letters
  17. number = '1234567890'
  18. punctuation = '~!@#$%^&*()_=-/\,.?<>;:[]{}|'


  19. def checking(password):
  20.     str(password)
  21.     if len(password) <= 8 and (password.isalpha() == 0 or password.isnumeric() == 0):
  22.         return '低级密码'

  23.     length = len(password)
  24.     print(length)
  25.     word_check = 0
  26.     number_check = 0
  27.     punctuation_check = 0
  28.     for i in range(0, length + 1):
  29.         cache = password[i:i + 1]
  30.         if cache in word:
  31.             word_check = 1
  32.             print('wordfound')
  33.         if cache in number:
  34.             number_check = 1
  35.             print('numberfound')
  36.         if cache in punctuation:
  37.             punctuation_check = 1
  38.             print('punctuationfound')
  39.     if word_check + number_check + punctuation_check == 2 and length >= 8:
  40.         return '中级密码'
  41.     elif word_check + number_check + punctuation_check == 3 and length >= 16 and password[0:1] in word:
  42.         return '高级密码'
  43.     else:
  44.         print(word_check,number_check,punctuation_check)
  45.         return '不符合规定'


  46. if __name__ == '__main__':
  47.     password = input('请输入密码')
  48.     print('正在检查')
  49.     print(checking(password))
复制代码

调试数据是这样的
  1. 请输入密码123456789
  2. 正在检查
  3. 9
  4. numberfound
  5. numberfound
  6. numberfound
  7. numberfound
  8. numberfound
  9. numberfound
  10. numberfound
  11. numberfound
  12. numberfound
  13. wordfound #这三个哪里来的
  14. numberfound #这三个哪里来的
  15. punctuationfound #这三个哪里来的
  16. 1 1 1
  17. 不符合规定

  18. 进程已结束,退出代码为 0
复制代码

有大佬帮我下吗?苦思不得其解啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-6 01:26:59 | 显示全部楼层
本帖最后由 阿奇_o 于 2021-7-6 01:30 编辑
  1. import re

  2. def passwd_test(pw):

  3.     # 低强度
  4.     if re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw):
  5.         # print(re.fullmatch(r"(\d{1,8})|(\w{1,8})", pw))
  6.         return "弱密码"

  7.     # 中强度
  8.     # https://gist.github.com/leadfast/1932460490b637260b92ad9a200d4eac
  9.     pt0 = re.compile(r"(?=^.{8,15}$)(?=(?:.*?\d){1})(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
  10.     pt1 = re.compile(r"(?=^.{8,}$)(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$") # 字母和特殊字符的组合,无数字
  11.     pt2 = re.compile(r"(?=^.{8,}$)(?=(?:.*?\d){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")  # 数字和特殊字符组合,无字母
  12.     pt3 = re.compile(r"\d\w{8,}")  # 数字和字母组合,无特殊字符
  13.     for p in [pt0, pt1, pt2, pt3]:
  14.         if re.search(p, pw):
  15.             return "中强度"

  16.     # 高强度
  17.     pt = re.compile(r"(?=^[a-zA-Z]{1,}.{15,}$)(?=(?:.*?\d){1})(?=(?:.*?[a-zA-Z]){1})(?=(?:.*?[-~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]){1})[-0-9a-zA-Z~!@#\$%\^&\*\(\)_=\/\,.\?<>;:\[\]\{\}\|\\]*$")
  18.     if re.search(pt, pw):
  19.         return "强密码"

复制代码


print(passwd_test('1234678'))
print(passwd_test('123z678@'))
print(passwd_test('password1234!@#'))   # 中强度,位数不够
print(passwd_test('password1234!@#$'))   # 强密码,刚够16位
print(passwd_test('password1234!@#$.'))  # 强密码
print(passwd_test('100password1234!@#$.')) # 中强度,不是字母开头


正则。。you make me crazy!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-6 09:06:10 | 显示全部楼层
  1. for i in range(0, length + 1):        #  改成  for i in range(0, length):
  2.         cache = password[i:i + 1]            #  改成password[i]  
复制代码


数组下标越界所致,你仔细数一数,实际上已经输出了9次正确结果
第10次的时候 cache = password[i:i + 1]    实际上 cache还被赋予了一次值,  值的结果为 ''  
然后因为你的  if 为并列关系,所以  cache   匹配了3次。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-4 14:42:07 | 显示全部楼层
  1. caps = [i for i in range(65, 91)]+[i for i in range(97, 123)]
  2. digits = [i for i in range(48, 58)]
  3. symbols = [i for i in range(33, 48)]+[i for i in range(58, 65)]+[91, 92, 93, 94, 95, 96, 123, 124, 125]

  4. def isHigh(p: 'passwords') -> bool:
  5.     if len(p) < 16:
  6.         return False
  7.     elif ord(p[0]) not in caps:
  8.         return False
  9.     return (any([bool(ord(i) in caps) for i in p])\
  10.         and any([bool(ord(i) in digits) for i in p])\
  11.             and any([bool(ord(i) in symbols) for i in p]))

  12. def isMedium(p: 'passwords') -> bool:
  13.     if len(p) < 8:
  14.         return False
  15.     x = any([bool(ord(i) in caps) for i in p])
  16.     y = any([bool(ord(i) in digits) for i in p])
  17.     z = any([bool(ord(i) in symbols) for i in p])
  18.     a = x and y and (not z)
  19.     b = x and z and (not y)
  20.     c = y and z and (not x)
  21.     return any([a, b, c])

  22. def isLow(p: 'passwords') -> bool:
  23.     if len(p) > 8:
  24.         return False
  25.     x = any([bool(ord(i) in caps) for i in p])
  26.     y = any([bool(ord(i) in digits) for i in p])
  27.     z = any([bool(ord(i) in symbols) for i in p])
  28.     return y and (not x) and (not z)

  29. def check(p: 'passwrods') -> str:
  30.     if isHigh(p):
  31.         return "高級密碼"
  32.     elif isMedium(p):
  33.         return "中級密碼"
  34.     elif isLow(p):
  35.         return "低級密碼"
  36.     else:
  37.         return "密碼不合法"

  38. def main():
  39.     print(check(input("請輸入密碼:\n")))

  40. if __name__ == "__main__":
  41.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 18:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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