Python破解pdf打开密码

20 篇文章 9 订阅
本文介绍了如何在Ubuntu系统(uos)上利用Python的PyPDF2、pikepdf和tqdm库来尝试破解PDF文件的打开密码。通过读取密码字典(如rockyou.txt)并逐个尝试,最终成功找到密码为'bengie04'。文章强调了破解成功率和速度取决于密码的复杂性和字典的大小,简单密码更容易破解,而复杂密码可能需要更长时间甚至无法破解。
摘要由CSDN通过智能技术生成

Python破解pdf打开密码

环境版本
系统uos
Python版本Python3
模块PyPDF2、pikepdf、tqdm

安装相应的模块

sudo pip3 install PyPDF2
sudo pip3 install pikepdf
sudo pip3 install tqdm

运行破解代码

import pikepdf
import os
from PyPDF2 import PdfFileReader
from tqdm import tqdm
filename = "/home/uos/Desktop/PyQt5入门.pdf" #pdf文件路径
wordlist = "/home/uos/Desktop/wordlists/rockyou.txt" #密码字典路径
n_words = len(list(open(wordlist, 'rb')))
fp = open(filename, "rb+")
pdfFile = PdfFileReader(fp)
filepath, tempfilename = os.path.split(filename)
with open(wordlist, "rb") as wordlist:
    if pdfFile.isEncrypted:
        for word in tqdm(wordlist, total=n_words, unit="word"):
            try:
                pdf = pikepdf.open(filename, password=word.strip())
            except:
                continue
            else:
                print("[+] Password found:", word.decode().strip())
                exit(0)
        print("[!] Password not found, try other wordlist!")

运行结果
密码为:bengie04

/home/uos/PycharmProjects/pythonProject1/venv/bin/python /home/uos/PycharmProjects/pythonProject1/main.py
  7%|| 1011828/14344392 [22:20<4:54:19, 754.96word/s]
[+] Password found: bengie04

Process finished with exit code 0
  1. 密码字典获取
    可以在kali系统的/usr/share/wordlists目录下拷贝一份到uos系统中或者百度rockyou.txt文件
  2. 是否可以100%破解
    破解速度与成功率和压缩密码的长度和复杂度有关,简单密码比较容易破解,复杂密码破解时间比较长甚至会破解失败
  3. 做实验时尽量选取密码字典中开头的密码以节约时间
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要清除PDF密码,可以使用Python中的PyPDF2库来完成。下面是一个使用PyPDF2库的示例代码: ``` import PyPDF2 def remove_pdf_password(input_path, output_path, password): # 打开加密的PDF文件 with open(input_path, 'rb') as input_file: pdf_reader = PyPDF2.PdfFileReader(input_file) # 检查PDF文件是否被加密 if pdf_reader.isEncrypted: # 解密PDF文件 pdf_reader.decrypt(password) # 新建一个空的PDF写入对象 pdf_writer = PyPDF2.PdfFileWriter() # 将每一页的内容复制到新的PDF对象中 for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) pdf_writer.addPage(page) # 将新的PDF内容写入输出文件 with open(output_path, 'wb') as output_file: pdf_writer.write(output_file) # 调用函数来清除PDF密码 input_path = 'encrypted.pdf' output_path = 'decrypted.pdf' password = 'password123' remove_pdf_password(input_path, output_path, password) ``` 在这个示例中,我们首先打开加密的PDF文件,然后使用`decrypt()`方法来解密文件。然后我们使用`PdfFileWriter`对象创建一个新的空白PDF文件,并使用`addPage()`方法将每一页的内容复制到新的PDF中。最后,我们将新的PDF内容写入输出文件。记得将`input_path`替换为你的输入文件路径,将`output_path`替换为你的输出文件路径,将`password`替换为你的PDF密码。 请注意,使用此方法需要安装PyPDF2库,即在命令行中运行`pip install PyPDF2`。此外,此方法只能用于清除已知密码PDF文件。如果PDF文件有未知密码或只允许特定的用户访问,将无法使用此方法来清除密码
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值