【天天學點Python】案例六:判斷密碼強弱

判斷密碼強弱

V1.0

案例描述:

  • 密碼強度:是指一個密碼對抗猜想或時暴力破解的有效程度;通常是指一個未受權的訪問者獲得正確密碼的平均嘗試次數
  • 強密碼能夠下降安全漏洞的總體風險
  • 簡易版(經常使用)規則:
    1. 密碼長度至少8位
    2. 密碼含有數字
    3. 密碼含有字母

案例分析:

  • 設置一個變量strength_level用於記錄密碼的強度,初始爲0。知足一個條件,對其加1
  • 長度判斷:使用len()方法
  • 包含數字判斷:使用snumeric()方法
  • 包含字母判斷:使用salpha()方法
  • 若是strength_level等於3,密碼強度合格,不然不合格

上機實驗:

 1 """
 2     做者:王鑫正
 3     版本:1.0
 4     日期:2018/9/26
 5     功能:判斷密碼強弱
 6 """
 7 
 8 
 9 def check_number_exist(password_str):
10     """
11         判斷字符串中是否含有數字
12     """
13     for c in password_str:
14         if c.isnumeric():
15             return True
16     return False
17 
18 
19 def check_letter_exist(password_str):
20     """
21         判斷字符串中是否含有字母
22     """
23     for c in password_str:
24         if c.isalpha():
25             return True
26     return False
27 
28 
29 def main():
30     """
31         主函數
32     """
33     password = input('請輸入密碼:')
34 
35     # 密碼強度
36     strength_level = 0
37 
38     # 規則1:密碼長度大於8
39     if len(password) >= 8:
40         strength_level += 1
41     else:
42         print('密碼長度要求至少8位')
43 
44     # 規則2:密碼長達包含數字
45     if check_number_exist(password):
46         strength_level += 1
47     else:
48         print('密碼要求包含數字')
49 
50     # 規則3:密碼長達包含字母
51     if check_letter_exist(password):
52         strength_level += 1
53     else:
54         print('密碼要求包含字母')
55 
56     if strength_level == 3:
57         print('恭喜!密碼強度合格')
58     else:
59         print('密碼強度不合格')
60 
61 
62 if __name__ == '__main__':
63     main()
View Code

補充說明:

1. Python字符串python

  • str.isnumeric():檢測字符串是否只由數字組成
  • str.isalpha():檢測字符串是否只由字母組成
  • str.islower():檢測字符串中全部的字母是否都爲小寫
  • str.isupper():檢測字符串中全部的字母是否都爲大寫

 


V2.0增長功能:限制密碼設置次數;循環的終止

案例分析:

  • 若是用戶在規定次數內設置符合要求的密碼,則終止循環

上機實驗:

 1 """
 2     做者:王鑫正
 3     版本:2.0
 4     日期:2018/9/26
 5     功能:判斷密碼強弱
 6     2.0增長功能:限制密碼設置次數;循環的終止
 7 """
 8 
 9 
10 def check_number_exist(password_str):
11     """
12         判斷字符串中是否含有數字
13     """
14     has_number = False
15 
16     for c in password_str:
17         if c.isnumeric():
18             has_number = True
19             break
20 
21     return has_number
22 
23 
24 def check_letter_exist(password_str):
25     """
26         判斷字符串中是否含有字母
27     """
28     has_letter = False
29 
30     for c in password_str:
31         if c.isalpha():
32             has_letter = True
33             break
34 
35     return has_letter
36 
37 
38 def main():
39     """
40         主函數
41     """
42     try_timer = 5
43 
44     while try_timer > 0:
45         password = input('請輸入密碼:')
46 
47         # 密碼強度
48         strength_level = 0
49 
50         # 規則1:密碼長度大於8
51         if len(password) >= 8:
52             strength_level += 1
53         else:
54             print('密碼長度要求至少8位')
55 
56         # 規則2:密碼長達包含數字
57         if check_number_exist(password):
58             strength_level += 1
59         else:
60             print('密碼要求包含數字')
61 
62         # 規則3:密碼長達包含字母
63         if check_letter_exist(password):
64             strength_level += 1
65         else:
66             print('密碼要求包含字母')
67 
68         if strength_level == 3:
69             print('恭喜!密碼強度合格')
70             break
71         else:
72             print('密碼強度不合格')
73             try_timer -= 1
74 
75         print()
76 
77     if try_timer <= 0:
78         print('嘗試次數過多,密碼設置失敗!')
79 
80 
81 if __name__ == '__main__':
82     main()
View Code

補充說明:

1. 循環的終止編程

  • break語句:終止整個循環
  • continue語句:只終止本次循環,而不終止整個循環的執行

 


V3.0增長功能: 保存設置的密碼及其對用的強度到文件中

上機實驗:

 1 """
 2     做者:王鑫正
 3     版本:3.0
 4     日期:2018/9/26
 5     功能:判斷密碼強弱
 6     3.0增長功能: 保存設置的密碼及其對用的強度到文件中
 7 """
 8 
 9 
10 def check_number_exist(password_str):
11     """
12         判斷字符串中是否含有數字
13     """
14     has_number = False
15 
16     for c in password_str:
17         if c.isnumeric():
18             has_number = True
19             break
20 
21     return has_number
22 
23 
24 def check_letter_exist(password_str):
25     """
26         判斷字符串中是否含有字母
27     """
28     has_letter = False
29 
30     for c in password_str:
31         if c.isalpha():
32             has_letter = True
33             break
34 
35     return has_letter
36 
37 
38 def main():
39     """
40         主函數
41     """
42     try_timer = 5
43 
44     while try_timer > 0:
45         password = input('請輸入密碼:')
46 
47         # 密碼強度
48         strength_level = 0
49 
50         # 規則1:密碼長度大於8
51         if len(password) >= 8:
52             strength_level += 1
53         else:
54             print('密碼長度要求至少8位')
55 
56         # 規則2:密碼長達包含數字
57         if check_number_exist(password):
58             strength_level += 1
59         else:
60             print('密碼要求包含數字')
61 
62         # 規則3:密碼長達包含字母
63         if check_letter_exist(password):
64             strength_level += 1
65         else:
66             print('密碼要求包含字母')
67 
68         f = open('password_3.0.txt', 'a')
69         f.write('密碼:{},強度:{}\n'.format(password, strength_level))
70         f.close()
71 
72         if strength_level == 3:
73             print('恭喜!密碼強度合格')
74             break
75         else:
76             print('密碼強度不合格')
77             try_timer -= 1
78 
79         print()
80 
81     if try_timer <= 0:
82         print('嘗試次數過多,密碼設置失敗!')
83 
84 
85 if __name__ == '__main__':
86     main()
View Code

補充說明:

1. 文件的基礎安全

  • 文件:存儲在外部介質(如:硬盤)上的數據或信息的集合
  • 文本文件:通常指只有字符編碼存儲的文件,可以被最簡單的文本編輯器直接讀取
  • 編碼:信息從一種形式轉換爲另外一種形式的過程
  • 經常使用的編碼:ASCⅡ,Unicode,UTF-8
  • 多行文本,用\n表示換行

2. 文件的操做編輯器

  • 步驟:打開文件->操做文件(讀、寫等)->關閉文件
  • 打開文件:創建文件與程序的關聯
    • open(filename,mode)
    • filename:文件名(包括路徑)
    • mode:打開模式
      1. r:只讀,文件不存在則報錯
      2. w:只寫,文件不存在則自動建立
      3. a:在文件末尾附加
      4. r+:讀寫
  • 操做文件:寫入、讀取等
    • 寫入操做:從計算機內存向文件寫入數據
    • write():將文本數據寫入文件中
    • writelines():將字符串列表寫入文件中
  • 關閉文件:終止程序與文件的關聯
    • close()

 


V4.0增長功能:讀取保存的密碼

 上機實驗:

 1 def main():
 2     """
 3         主函數
 4     """
 5 
 6     # 讀取文件
 7     f = open('password_3.0.txt', 'r')
 8 
 9     # 1. read()
10     content = f.read()
11     print(content)
12 
13     # 2. readline()
14     line = f.readline()
15     print(line)
16 
17     # 3. readlines()
18     for line in f:
19         print('read:{}'.format(line))
20 
21     for line in f.readlines():
22         print('read:{}'.format(line))
23 
24     f.close()
25 
26 
27 if __name__ == '__main__':
28     main()
View Code

補充說明:

1. 文件的操做ide

  • 讀取操做:從文件中讀取數據到計算機內存中
  • read():返回值爲包含整個文件內容的一個字符串
  • readline():返回值爲文件下一行內容的字符串
  • readlines():返回值爲整個文件內容的列表,每項是以換行符爲結尾的一行字符串

2. 文件的遍歷:函數

1     for line in f:
2         print('read:{}'.format(line))
3 
4     for line in f.readlines():
5         print('read:{}'.format(line))

 


V5.0增長功能:將相關方法封裝成一個總體:面向對象編程

上機實驗:

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # author: Kevin.Wang
  4 # time  : 2018/9/26
  5 
  6 
  7 """
  8     做者:王鑫正
  9     版本:5.0
 10     日期:2018/9/26
 11     功能:判斷密碼強弱
 12     5.0增長功能:定義一個password工具類
 13 """
 14 
 15 
 16 class PasswordTool:
 17     """
 18         密碼工具類
 19     """
 20     def __init__(self, password):
 21         # 類的屬性
 22         self.password = password
 23         self.strength_level = 0
 24 
 25     # 類的方法
 26     def process_password(self):
 27         # 規則1:密碼長度大於8
 28         if len(self.password) >= 8:
 29             self.strength_level += 1
 30         else:
 31             print('密碼長度要求至少8位')
 32 
 33         # 規則2:密碼長達包含數字
 34         if self.check_number_exist():
 35             self.strength_level += 1
 36         else:
 37             print('密碼要求包含數字')
 38 
 39         # 規則3:密碼長達包含字母
 40         if self.check_letter_exist():
 41             self.strength_level += 1
 42         else:
 43             print('密碼要求包含字母')
 44 
 45     def check_number_exist(self):
 46         """
 47             判斷字符串中是否含有數字
 48         """
 49         has_number = False
 50 
 51         for c in self.password:
 52             if c.isnumeric():
 53                 has_number = True
 54                 break
 55 
 56         return has_number
 57 
 58     def check_letter_exist(self):
 59         """
 60             判斷字符串中是否含有字母
 61         """
 62         has_letter = False
 63 
 64         for c in self.password:
 65             if c.isalpha():
 66                 has_letter = True
 67                 break
 68 
 69         return has_letter
 70 
 71 
 72 def main():
 73     """
 74         主函數
 75     """
 76     try_timer = 5
 77 
 78     while try_timer > 0:
 79         password = input('請輸入密碼:')
 80 
 81         # 實例化密碼工具對象
 82         password_tool = PasswordTool(password)
 83         password_tool.process_password()
 84 
 85         f = open('password_5.0.txt', 'a')
 86         f.write('密碼:{},強度:{}\n'.format(password, password_tool.strength_level))
 87         f.close()
 88 
 89         if password_tool.strength_level == 3:
 90             print('恭喜!密碼強度合格')
 91             break
 92         else:
 93             print('密碼強度不合格')
 94             try_timer -= 1
 95 
 96         print()
 97 
 98     if try_timer <= 0:
 99         print('嘗試次數過多,密碼設置失敗!')
100 
101 
102 if __name__ == '__main__':
103     main()
View Code

補充說明:

1. 面向過程 vs 面向對象工具

  • 面向過程(POP):以程序執行過程爲設計流程的編程思想
  • 面向對象(OOP):以事物爲中心的編程思想
  • 什麼是對象(Object)?
  • 現實世界中的對象:屬性,行爲
  • 對象例子:
    • 波斯貓,屬性:品種、顏色、大小;行爲:叫、捉老鼠
    • 吉普車,屬性:類型、用途;行爲:發動、停車
  • 類(class):某種類型集合的描述
  • 屬性:類自己的一些特性
  • 方法:類所能實現的行爲
  • 類的定義:
    • class ClassName
    • __init__(self) 構造函數:初始化對象的各屬性
    • self表明類的實例

 


V6.0增長功能:將文件操做封裝到一個類中

上機實驗:

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # author: Kevin.Wang
  4 # time  : 2018/9/26
  5 
  6 
  7 """
  8     做者:王鑫正
  9     版本:6.0
 10     日期:2018/9/26
 11     功能:判斷密碼強弱
 12     6.0增長功能:將文件操做封裝到一個類中
 13 """
 14 
 15 
 16 class FileTools:
 17     """
 18         文件工具類
 19     """
 20     def __init__(self,filepath):
 21         self.filepath = filepath
 22 
 23     def write_to_file(self, line):
 24         f = open(self.filepath, 'a')
 25         f.write(line)
 26         f.close()
 27 
 28     def read_form_file(self):
 29         f = open(self.filepath, 'r')
 30         lines = f.readlines()
 31         f.close()
 32         return lines
 33 
 34 
 35 class PasswordTool:
 36     """
 37         密碼工具類
 38     """
 39     def __init__(self, password):
 40         # 類的屬性
 41         self.password = password
 42         self.strength_level = 0
 43 
 44     # 類的方法
 45     def process_password(self):
 46         # 規則1:密碼長度大於8
 47         if len(self.password) >= 8:
 48             self.strength_level += 1
 49         else:
 50             print('密碼長度要求至少8位')
 51 
 52         # 規則2:密碼長達包含數字
 53         if self.check_number_exist():
 54             self.strength_level += 1
 55         else:
 56             print('密碼要求包含數字')
 57 
 58         # 規則3:密碼長達包含字母
 59         if self.check_letter_exist():
 60             self.strength_level += 1
 61         else:
 62             print('密碼要求包含字母')
 63 
 64     def check_number_exist(self):
 65         """
 66             判斷字符串中是否含有數字
 67         """
 68         has_number = False
 69 
 70         for c in self.password:
 71             if c.isnumeric():
 72                 has_number = True
 73                 break
 74 
 75         return has_number
 76 
 77     def check_letter_exist(self):
 78         """
 79             判斷字符串中是否含有字母
 80         """
 81         has_letter = False
 82 
 83         for c in self.password:
 84             if c.isalpha():
 85                 has_letter = True
 86                 break
 87 
 88         return has_letter
 89 
 90 
 91 def main():
 92     """
 93         主函數
 94     """
 95     try_timer = 5
 96     file_path = 'password_6.0.txt'
 97     # 實例化文件工具對象
 98     file_tool = FileTools(file_path)
 99 
100     while try_timer > 0:
101         password = input('請輸入密碼:')
102 
103         # 實例化密碼工具對象
104         password_tool = PasswordTool(password)
105         password_tool.process_password()
106 
107         line = '密碼:{},強度:{}\n'.format(password, password_tool.strength_level)
108         # 寫文件
109         file_tool.write_to_file(line)
110 
111         if password_tool.strength_level == 3:
112             print('恭喜!密碼強度合格')
113             break
114         else:
115             print('密碼強度不合格')
116             try_timer -= 1
117 
118         print()
119 
120     if try_timer <= 0:
121         print('嘗試次數過多,密碼設置失敗!')
122 
123     # 讀操做
124     lines = file_tool.read_form_file()
125     print(lines)
126 
127 
128 if __name__ == '__main__':
129     main()
View Code

補充說明:

1. 面向對象的特色編碼

  • 封裝
    • 將數據及相關操做打包在一塊兒
    • 支持代碼複用
  • 繼承
    • 子類(subclass)借用父類(superclass)的行爲
    • 避免重複操做,提高代碼複用程度
    • 定義 class ClassName(SuperClassName)
  • 多態
    • 在不一樣狀況下用一個函數名啓用不一樣方法
    • 靈活性
相關文章
相關標籤/搜索