如何使我的 Python 密码破解程序更有效地运行?

How to make my Python password cracker operate more efficiently?

不久前我对制作一个伪密码破解器产生了兴趣。 所以,这里有一些代码:

list = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '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'] # giving it a list 

passlength = int(input('Length of string: ')) # length of code or no of objects
aspass = '' # empty string acts as assumed password
passs = input('Please input password ') # he infamous password
b = [] # the list that will stores randomly generated passwords as values
attempt = 0
digits = 0   
o = True
while o:
    for k in range(0, passlength): # run this loop as many times as the length of password
        x = str(random.choice(list))#the attempted upon digit in the password
        aspass += x
        digits += 1 # counts the step the cracker is on
        #print(aspass)
        if(len(aspass) > passlength or aspass in b):
            digits = 0
            attempt += 1
            break
        else:
            continue
        #b.append(aspass)
    if(aspass == passs):
        break
        o = False
        end()
    else:
        b.append(aspass)
        aspass = ''
        continue

这里的事情是,一切正常,它生成 2 个字符串密码。但是,如果长度超过 2 或 3 个字符串。好吧,它以蜗牛的​​速度移动。然后我想到了一个主意,如果我可以将随机生成的密码保存在我制作的 "b" 列表中,并确保该列表中的密码在此过程中不会重复,那么我认为它会 运行明显更快。

因为我完全是个初学者,所以我不知道如何用其他方式让它更快。我可以尝试哪些事情(例如可导入模块)来改进它?

密码破解不是一件容易的事。想一想随着密码长度的增加,您必须进行的搜索 space。您的可能字符列表包含 26 个字母和 10 个数字(顺便说一句,您可以使用 string.digitsstring.ascii_lowercase)。因此,对于密码中的第一个字符,有 36 个选项。第二个有 36 个选项,第三个有 36 个选项,依此类推。因此,对于长度为 n 的密码,您将有 3^n 个选项。正如您很快就会看到的那样,这个数字增长得非常快,即使是很小的数字也是如此。

您破解密码的方法称为 Brute-force attack,它非常低效,尤其是考虑到大多数密码不是以纯文本形式存储,而是以散列字符串形式存储的事实。

一些其他注意事项:

  1. 你的变量名不太好。它们中的大多数都没有意义,这会使您的代码更难理解。
  2. 您 select 随机字符串,而不是按顺序遍历所有可能的选项。您无法使用此方法涵盖所有​​选项。您可以使用 itertools.permutations 遍历所有选项。
  3. 不要在 if 语句中使用括号,这不是 Python 的方式。请。

我制作了一个程序,我想制作 GUI,但如果你需要它,你可以从命令 lilne 使用它

 try:
    import string
except:
    print("please install strings lib with command <sudo pip3 install ")
try:
    import random
except:
    print("please install random lib with command <sudo pip3 install random>1")
try:
    import os
except:
    print("pleas install the os lib with command <sudo pip install os>")


def switch_case_no():
    dict_no = {"no": 0, "n": 0, "No": 0, "N": 0, "NO": 0}
    return dict_no


def switch_case_yes():
    dict_yes = {"yes": 1, "y": 1, "Yes": 1, "Y": 1, "YES": 1}
    return dict_yes


int_list = str(["1"+"2"+"3"+"4"+"5"+"6"+"7"+"8"+"9"+"0"])
sym_list = str(["~"+"!"+"@"+"#"+"$"+"%"+"^"+"&"+"*"+"("+")"+"_"+"+"+"|"+"?"+">"+"<"])
i = int_list
s = sym_list
l = string.ascii_lowercase
u = string.ascii_uppercase
list_int_1 = tuple(f"{i}")
list_sym_1 = tuple(f"{s}")
list_str_l_1 = tuple(f"{l}")
list_str_u_1 = tuple(f"{u}")


def checker(this_list):  # function to check and generate password list
    x = False  # loop broker
    while not x:  # start of the loop
        password = "".join(random.sample(this_list, k=len1))
        print(f">>>>>>!!!{password}!!!<<<<<<<\r", end="")
        if password == entry:
            print(f"the password is : " "\n""*****>>>>>>>>{password}<<<<<<<<*****\n created by PeterHattson")
            x = True
            try:
                os.system("beep -f 759 -l 400")
            except Exception:
                print("the system beep has not founded !!!!")
            break  # end of the while loop


entry = input("inter your pass >>>>  ")  # you can change this line with any login page that you want
ask_user_int = input("you password has an integer ??? ")
if ask_user_int in switch_case_yes():
    print(">>>>ckuF Your password contain with integers <<<<")
elif ask_user_int in switch_case_no():
    print(">>>>ckuF Your password will not contain with integers <<<<")
else:
    print("please just use yes/y or no/n ")
ask_user_sym = input("your password has a special symbols ??? ")
if ask_user_sym in switch_case_yes():
    print(">>>>ckuF Your password contain with symbols <<<<")
elif ask_user_sym in switch_case_no():
    print(">>>>ckuF Your password will not contain with symbols <<<<")
else:
    print("please just use yes/y or no/n ")
ask_user_str_l = input("did your password contain with lower case alphabet ????")
if ask_user_str_l in switch_case_yes():
    print(">>>>ckuF Your password contain with lower case alphabet <<<<")

elif ask_user_str_l in switch_case_no():
    print(">>>>ckuF Your password will not  contain with  lower case alphabet <<<<")
else:
    print("please just use yes/y or no/n ")
ask_user_str_u = input("did your password contain with upper case alphabet ????")
if ask_user_str_u in switch_case_yes():
    print(">>>>ckuF Your password contain with upper case alphabet  <<<<")
elif ask_user_str_u in switch_case_no():
    print(">>>>ckuF Your password will not  contain with upper case alphabet  <<<<")
else:
    print("please just use yes/y or no/n ")
len1 = len(entry)

if ask_user_int in switch_case_yes():
    if ask_user_sym in switch_case_yes():
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_no():
                checker(list_int_1 + list_sym_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_no():
                checker(list_int_1 + list_str_l_1 + list_sym_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_yes():
                checker(list_int_1 + list_str_l_1 + list_str_u_1 + list_sym_1)
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_yes():
                checker(list_int_1 + list_str_u_1 + list_sym_1)
if ask_user_int in switch_case_no():
    if ask_user_sym in switch_case_yes():
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_no():
                checker(list_sym_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_no():
                checker(list_str_l_1 + list_sym_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_l_1 + list_str_u_1 + list_sym_1)
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_u_1 + list_sym_1)
if ask_user_int in switch_case_yes():
    if ask_user_sym in switch_case_no():
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_no():
                checker(list_int_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_no():
                checker(list_str_l_1 + list_int_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_l_1 + list_str_u_1 + list_int_1)
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_u_1 + list_int_1)
if ask_user_int in switch_case_no():
    if ask_user_sym in switch_case_no():
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_no():
                print("Your pass list is empty!!!!")
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_no():
                checker(list_str_l_1)
        if ask_user_str_l in switch_case_yes():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_l_1 + list_str_u_1)
        if ask_user_str_l in switch_case_no():
            if ask_user_str_u in switch_case_yes():
                checker(list_str_u_1)

这个怎么样:

import pyautogui
import time
import random

chars = "abcdefghijklmnopqrstuvwxyz"
chars_list = list(chars)

password = pyautogui.password("Enter a Password : ")

guess_password = ""

while(guess_password != password):
    guess_password = random.choices(chars_list, k=len(password))

    print("DECRYPTING"+ str(guess_password)+ "PASSWORD")

    if(guess_password == list(password)):
        print("Your password is : "+ "".join(guess_password))
        break