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

2024-04-23 17:52:16 发布

您现在位置:Python中文网/ 问答频道 /正文

不久前,我对制作一个伪密码破解程序感兴趣。 下面是一些代码:

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”列表中,并确保该列表中的密码在这个过程中不会重复,那么我认为它会运行得更快

由于我是一个完全的初学者,我不知道如何以其他方式使它更快。我可以尝试哪些方法(例如可导入的模块)来改进这一点


Tags: orofthein密码inputstringas
3条回答

破解密码并非易事。考虑一下随着密码长度的增长,您必须通过的搜索空间。您的可能字符列表包含26个字母和10个数字(顺便说一下,您可以使用string.digitsstring.ascii_lowercase)。因此,对于密码中的第一个字符,有36个选项。第二个有36个选项,第三个有36个选项,依此类推。因此,对于长度为n的密码,您将有3^n个选项。正如你很快就能看到的,这个数字正在以极快的速度增长,即使是小数字

破解密码的方法称为Brute-force attack,效率极低,特别是考虑到大多数密码不是以明文形式存储的,而是以散列字符串形式存储的

其他注释很少:

  1. 你的名字不太好。它们中的大多数都是无意义的,这使得代码更难理解
  2. 您选择随机字符串,而不是按顺序浏览所有可能的选项。使用此方法无法覆盖所有选项。您可以使用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

相关问题 更多 >