首頁 > 軟體

python如何生成密碼字典

2022-03-08 19:00:35

一、密碼字典

所謂密碼字典,主要是配合解密使用,一般情況用來暴力破解密碼,是由指定字元排列組合組成的文字檔案。如果知道密碼設定的規律指定性生成密碼,會對破解密碼有決定性的幫助!!

二、字典生成

1.生成6位數小寫字母+數位密碼字典

程式碼如下(範例):

import itertools as its

words = 'abcdefghijklmnopqrstuvwxyz1234567890'  #採用的字元

r = its.product(words, repeat=6)  # repeat 要生成多少位的字典

dic = open("pass.txt", "a")    #儲存
for i in r:
    dic.write("".join(i))
    dic.write("".join("r"))
dic.close()

2.選擇模式執行

python dictionary.py default
python dictionary.py numonly
python dictionary.py letteronly

程式碼如下(範例):

import itertools as its
import argparse
def run_default(length,filename):
    global words
    '''
    words='ha'
    
    if numonly == True:
        words="1234567890"
    else:
        words="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
    '''
    words="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
    r =its.product(words,repeat=length)
    dic = open(filename,'a')
    for i in r:
        dic.write("".join(i))
        dic.write("".join("n"))
    dic.close()

def run_numonly(length,filename):
    global words
    words="1234567890"
    r =its.product(words,repeat=length)
    dic = open(filename,'a')
    for i in r:
        dic.write("".join(i))
        dic.write("".join("n"))
    dic.close()

def run_letteronly(length,filename):
    global words
    words="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
    r =its.product(words,repeat=length)
    dic = open(filename,'a')
    for i in r:
        dic.write("".join(i))
        dic.write("".join("n"))
    dic.close()

if __name__ == "__main__":
    choices={"default":run_default,"numonly":run_numonly,"letteronly":run_letteronly}
    parser=argparse.ArgumentParser(description='快速生成密碼字典')
    parser.add_argument('model',choices=choices,help='選擇哪個模式執行')
    parser.add_argument('--length',metavar='length',type=int,default=3,help="密碼字典內密碼的長度")
    parser.add_argument('-filename',metavar='filename',type=str,default='password.txt',help="密碼字典檔案暱稱")
    #parser.add_argument('-numonly',metavar='numonly',type=bool,default=False,help="是否只含有數位")
    args=parser.parse_args()
    func=choices[args.model]
    func(args.length,args.filename)

到此這篇關於python如何生成密碼字典的文章就介紹到這了,更多相關python密碼字典內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com