利用Python破解PDF文檔密碼(pyPDF2模塊並不可用)


""

開始嘗試用python的第三方庫pyPDF2來破解PDF文檔的密碼,結果運行的時候報錯:

 "NotImplementedError: only algorithm code 1 and 2 are supported" 提示不支持該加密算法。
 只能放棄這個模塊,哈哈,而且看了一下這個模塊的官方網站,似乎已經很久都沒有更新了!
 經過查詢,發現pikepdf模塊可以支持,廢話少說直接上代碼

 

""

from pikepdf import Pdf
import sys
import threading
import os

class PdfCracker:
    def __init__(self, pdf_file, password_list):
        self.pdf_file = pdf_file                                                                   #要被破解的PDF文檔文件名稱
        self.password_list = password_list                                               #密碼字典文件名稱
    

    def pdfer(self, password):
      
        try:
            Pdf.open(self.pdf_file, password=password)                            #Pdf的Open方法,如果密碼正確,那么可以執行,否則會報錯,因此可用except捕捉該錯誤
            print("Found Password: %s" % password)
            sys.exit()                                                                                   #如果找到文檔的密碼,那么也沒必要繼續執行下去了,直接退出程序
        except:
           
            pass
    
    def blank_password(self):                                                                #這個方法是在嘗試破解文檔密碼之前,確認一下該文檔是否被加密,如果不是,那么給出提示后,可以退出程序
        try:
            Pdf.open(self.pdf_file)
            print("The File is Not Encrypted!")
            return True
        except:
            return False

    def cracker(self):
        res = self.blank_password()
        if not res:           
            with open(self.password_list, 'r') as f:
                for line in f.readlines():
                    pass_try = line.strip('\n').strip()
                    t = threading.Thread(target=self.pdfer, args=(pass_try,))   # 用多線程加快破解過程
                    t.start()
        else:
            sys.exit()
    


if __name__ == '__main__':
    banner = """
                ****************************

                  PDF Brute Forcer By Jason

                ****************************
        """
    print(banner)
    
    pdf_file = input("Enter File Path To Crack: ")
    if not os.path.exists(pdf_file):
        print("The File Does Not Exist!")
        sys.exit()
    password_list = input("Enter File Path Of Dictionary: ")
    if not os.path.exists(password_list):
        print("The Dictionary Does Not Exist!")
        sys.exit()
    pdf_cracker = PdfCracker(pdf_file, password_list)
    pdf_cracker.cracker()

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM