1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
__author__ = 'QCF'
import os import platform import itertools import time
def main(): '''主程序''' global RawList RawList = [] global DenyList DenyList = [' ','','@'] global LastList LastList = [] global MinLen MinLen = 6 global MaxLen MaxLen = 16 global Timeout Timeout = 3 global Flag Flag = 0 RunDictionary = { '0':Exit, '1':GetRawList, '2':AddDenyList, '3':ClearRawList, '4':SetRawList, '5':ModifyPasswordLen, '6':CreatePasswordList, '7':ShowPassword, '8':CreatePasswordFile }
while True: MainMenu() op = input('输入选项:') if op in map(str,range(len(RunDictionary))): RunDictionary.get(op)() else: TipMainMenuInputError() continue
def MainMenu(): '''主菜单''' global RawList global DenyList global LastList global Flag Clear() print('||') print('=' * 40) print('||') print('|| 0:退出程序') print('|| 1:输入原始密码列表') print('|| 2:添加非法字符到列表') print('|| 3:清空原始密码列表') print('|| 4:设置原始密码列表') print('|| 5:改变默认密码长度({:d}-{:d})'.format(MinLen, MaxLen)) print('|| 6:创建密码列表') print('|| 7:显示所有密码') print('|| 8:创建密码字典文件') print('||') print('=' * 40) print('||') print('当前非法字符列表为:{}'.format(DenyList)) print('当前原始密码元素为:{}'.format(RawList)) print('共有密码{:d}个'.format(len(LastList))) if Flag: print('已在当前目录创建密码字典文件dic.txt') else: print('未创建密码字典文件')
def Clear(): '''清屏函数''' OS = platform.system() if (OS == u'Windiws'): os.system('cls') else: os.system('clear')
def TipMainMenuInputError(): '''错误提示''' Clear() print('只能输入0-7的整数,等待{:d}s后重新输入。'.format(Timeout)) time.sleep(Timeout)
def Exit(): exit()
def GetRawList(): '''获取原始密码列表''' Clear() global DenyList global RawList print('输入回车后直接退出') print('当前原始密码列表为:{}'.format(RawList)) st = None while not st == '': st = input('请输入密码元素字符串:') if st in DenyList: print('这个字符串是预先设定的非法字符串') continue else: RawList.append(st) Clear() print('输入回车后直接退出') print('当前原始密码列表为:{}'.format(RawList))
def AddDenyList(): '''添加非法字符串''' Clear() global DenyList print('输入回车后直接退出') print('当前非法字符为:{}'.format(DenyList)) st = None while not (st == ''): st = input('请输入需要添加的非法字符串:') DenyList.append(st) Clear() print('输入回车后直接退出') print('当前非法字符串列表为:{}'.format(DenyList))
def ClearRawList(): '''清空原始密码列表''' global RawList RawList = []
def SetRawList(): '''设置原始密码列表''' global RawList global DenyList a = set(RawList) b = set(DenyList) RawList = [] for str in set(a - b): RawList.append(str)
def ModifyPasswordLen(): '''修改默认密码长度''' Clear() global MinLen global MaxLen while True: print("当前密码长度为{:d},{:d}".format(MinLen, MaxLen)) min = input("请输入密码最小长度:") max = input("请输入密码最大长度:") try: MinLen = int(min) MaxLen = int(max) except ValueErrir: print("密码长度只能输入整数6-18") break if (MinLen not in range(6,19)) or (MaxLen not in range(6,19)): print("密码长度只能输入整数6-18") MinLen = 6 MaxLen = 16 continue if MinLen == MaxLen: res = input("确定将密码长度限定为{:d}吗?(Y/N)".format(MinLen)) if res not in list('YyNn'): print("输入错误,请重新输入!") continue elif res in list('Yy'): print("已将密码长度限定为{:d}".format(MinLen)) break else: pass elif MinLen > MaxLen: res = input("密码最小最大长度输入错误,是否交换最小最大长度?(Y/N)") if res not in list('YyNn'): print("输入错误,请重新输入!") continue elif res in list('Yy'): temp = MinLen MinLen = MaxLen Maxlen = temp print("密码长度为:({:d}-{:d})".format(MinLen, MaxLen)) else: print("密码最小最大长度输入错误,请重新输入!") else: print("设置完毕,等待{:d}s后返回菜单。".format(Timeout)) time.sleep(Timeout) break
def CreatePasswordList(): '''创建密码列表''' global RawList global LastList global MaxLen global MinLen titleList = [] swapcaseList = [] for st in RawList: swapcaseList.append(st.swapcase()) titleList.append(st.title())
sub1 = [] sub2 = [] for st in set(RawList + titleList + swapcaseList): sub1.append(st) for i in range(2, len(sub1) + 1): sub2 += list(itertools.permutations(sub1,i)) for tup in sub2: PW = '' for subPW in tup: PW += subPW if len(PW) in range(MinLen, MaxLen + 1): LastList.append(PW) else: pass
def ShowPassword(): '''显示密码''' global LastList global Timeout for i in range(len(LastList)): if i % 4 == 0: print("%s\n" % LastList[i]) else: print("%s\t" % LastList[i]) print('\n') print("显示{:d}s,回到主菜单。".format(Timeout)) time.sleep(Timeout)
def CreatePasswordFile(): '''创建密码字典文件''' global Flag global LastList print("在当前目录下创建字典文件:dic.txt") time.sleep(Timeout) with open('./dic.txt','w+') as fp: for PW in LastList: fp.write(PW) fp.write('\n') Flag = 1
if __name__ == '__main__': main()
|