Skip to main content
Tweeted twitter.com/StackCodeReview/status/1085688262516191237
deleted 183 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Open encrypted PDF File without knowing the passwordfile by trying passwords from a word list

As an exercise, I wrote a program to encryptdecrypt a PDF File if the passwordfile. The only thing that is not known as an excercise.

The resitrictions to unblockabout the encryption password is, that it is a single englishEnglish word (Allall capital or lowerlowercase) was used to encrypt the PDF File.

The Programprogram uses a dictionary filedictionary file containing over 45000 words to unlock the file. It looks like this:

AARHUS
AARON
ABABA
ABACK
ABAFT
ABANDON
ABANDONED
ABANDONING
ABANDONMENT
ABANDONS
ABASE
ABASED
ABASEMENT
ABASEMENTS
ABASES
ABASH
ABASHED
ABASHES
 

ZEROTH
ZEST
ZEUS
ZIEGFELD
ZIEGFELDS
ZIEGLER
ZIGGY
ZIGZAG
ZILLIONS
ZIMMERMAN
ZINC
ZION
ZIONISM
ZIONIST
ZIONISTS
ZIONS
ZODIAC
ZOE
ZOMBA
ZONAL
ZONALLY
ZONE
ZONED
ZONES
ZONING
ZOO
ZOOLOGICAL
ZOOLOGICALLY
ZOOM
ZOOMS
ZOOS
ZORN
ZOROASTER
ZOROASTRIAN
ZULU
ZULUS
ZURICH

The real file contains over 45000 words.It can be found here if you want to try out.

pdf_password_breaker

I tried a file with a simple password like "hello". It works but it takes alota lot of time until it reaches to the hello"hello" in the file.

I wonder if theres a way to improve the speed.

Also i wonder what can be in general improved in, or any other aspect of the code please let me know.

Open encrypted PDF File without knowing the password

I wrote a program to encrypt a PDF File if the password is not known as an excercise.

The resitrictions to unblock the password is, that a single english word (All capital or lower) was used to encrypt the PDF File.

The Program uses a dictionary file to unlock the file. It looks like this:

AARHUS
AARON
ABABA
ABACK
ABAFT
ABANDON
ABANDONED
ABANDONING
ABANDONMENT
ABANDONS
ABASE
ABASED
ABASEMENT
ABASEMENTS
ABASES
ABASH
ABASHED
ABASHES
 

ZEROTH
ZEST
ZEUS
ZIEGFELD
ZIEGFELDS
ZIEGLER
ZIGGY
ZIGZAG
ZILLIONS
ZIMMERMAN
ZINC
ZION
ZIONISM
ZIONIST
ZIONISTS
ZIONS
ZODIAC
ZOE
ZOMBA
ZONAL
ZONALLY
ZONE
ZONED
ZONES
ZONING
ZOO
ZOOLOGICAL
ZOOLOGICALLY
ZOOM
ZOOMS
ZOOS
ZORN
ZOROASTER
ZOROASTRIAN
ZULU
ZULUS
ZURICH

The real file contains over 45000 words.It can be found here if you want to try out.

pdf_password_breaker

I tried a file with a simple password like "hello". It works but it takes alot of time until it reaches to the hello in the file.

I wonder if theres a way to improve the speed.

Also i wonder what can be in general improved in the code please let me know.

Open encrypted PDF file by trying passwords from a word list

As an exercise, I wrote a program to decrypt a PDF file. The only thing that is known about the encryption password is that it is a single English word (all capital or lowercase).

The program uses a dictionary file containing over 45000 words to unlock the file. It looks like this:

AARHUS
AARON
ABABA
ABACK
ABAFT
ABANDON
ABANDONED
ABANDONING
ABANDONMENT
ABANDONS
ABASE
ABASED
ABASEMENT
ABASEMENTS
ABASES
ABASH
ABASHED
ABASHES

ZEROTH
ZEST
ZEUS
ZIEGFELD
ZIEGFELDS
ZIEGLER
ZIGGY
ZIGZAG
ZILLIONS
ZIMMERMAN
ZINC
ZION
ZIONISM
ZIONIST
ZIONISTS
ZIONS
ZODIAC
ZOE
ZOMBA
ZONAL
ZONALLY
ZONE
ZONED
ZONES
ZONING
ZOO
ZOOLOGICAL
ZOOLOGICALLY
ZOOM
ZOOMS
ZOOS
ZORN
ZOROASTER
ZOROASTRIAN
ZULU
ZULUS
ZURICH

pdf_password_breaker

I tried a file with a simple password like "hello". It works but it takes a lot of time until it reaches the "hello" in the file.

I wonder if theres a way to improve the speed, or any other aspect of the code.

added 9 characters in body
Source Link
Sandro4912
  • 3.2k
  • 2
  • 23
  • 50

The real file contains over 45000 words.It can be found herehere if you want to try out.

The real file contains over 45000 words.It can be found here if you want to try out.

The real file contains over 45000 words.It can be found here if you want to try out.

Source Link
Sandro4912
  • 3.2k
  • 2
  • 23
  • 50

Open encrypted PDF File without knowing the password

I wrote a program to encrypt a PDF File if the password is not known as an excercise.

The resitrictions to unblock the password is, that a single english word (All capital or lower) was used to encrypt the PDF File.

The Program uses a dictionary file to unlock the file. It looks like this:

dictionary.txt

AARHUS
AARON
ABABA
ABACK
ABAFT
ABANDON
ABANDONED
ABANDONING
ABANDONMENT
ABANDONS
ABASE
ABASED
ABASEMENT
ABASEMENTS
ABASES
ABASH
ABASHED
ABASHES


ZEROTH
ZEST
ZEUS
ZIEGFELD
ZIEGFELDS
ZIEGLER
ZIGGY
ZIGZAG
ZILLIONS
ZIMMERMAN
ZINC
ZION
ZIONISM
ZIONIST
ZIONISTS
ZIONS
ZODIAC
ZOE
ZOMBA
ZONAL
ZONALLY
ZONE
ZONED
ZONES
ZONING
ZOO
ZOOLOGICAL
ZOOLOGICALLY
ZOOM
ZOOMS
ZOOS
ZORN
ZOROASTER
ZOROASTRIAN
ZULU
ZULUS
ZURICH

The real file contains over 45000 words.It can be found here if you want to try out.

pdf_password_breaker

"""
Brute force password breaker using a dictionary containing
English words.
"""

import sys
import PyPDF2
from pathlib import Path

def get_filename_from_user() -> Path:
    """Asks for a path from the User"""
    while True:
        filename: str = input("Enter filename in folder of script:")
        path: Path = Path(sys.path[0], filename)

        if path.is_file():
            return path.as_posix()
        print("File doesn't exist\n")


def decrypt(pdf_filename: Path, password: str) -> bool:
    """
    Try to decrypt a file. If not successful a false is returned.
    If the file passed is not encrypted also a false is passed
    """
    with open(pdf_filename, 'rb') as pdf_file:
        pdf_reader = PyPDF2.PdfFileReader(pdf_file)
        pdf_reader.decrypt(password)
        pdf_writer = PyPDF2.PdfFileWriter()

        try:
            for page_number in range(pdf_reader.numPages):
                pdf_writer.addPage(pdf_reader.getPage(page_number))
        except PyPDF2.utils.PdfReadError:
            return False

        new_name: str = pdf_filename.stem + "_decrypted.pdf"
        filename_decrypted = pdf_filename.parent / new_name

        with open(filename_decrypted, 'wb') as pdf_file_decrypted:
            pdf_writer.write(pdf_file_decrypted)
    return True


def break_encryption(pdf_filename: Path, dictionary_filename: str) -> bool:
    """Try's out words from a dictionary to break encryption"""
    with open(dictionary_filename, 'r') as dictionary_file:
        keyword: str = dictionary_file.readline().strip()

        if decrypt(pdf_filename, keyword):
            return keyword
        if decrypt(pdf_filename, keyword.lower()):
            return keyword.lower()

        while keyword:
            keyword = dictionary_file.readline().strip()

            if decrypt(pdf_filename, keyword):
                return keyword
            if decrypt(pdf_filename, keyword.lower()):
                return keyword.lower()
    return None


def pdf_password_breaker():
    """main loop"""
    filename: Path = get_filename_from_user()
    password: str = break_encryption(filename, "dictionary.txt")

    if password:
        print("File unlocked. Password was:" + password)
        return
    print("File could not be unlocked")

if __name__ == "__main__":
    pdf_password_breaker()

I tried a file with a simple password like "hello". It works but it takes alot of time until it reaches to the hello in the file.

I wonder if theres a way to improve the speed.

Also i wonder what can be in general improved in the code please let me know.