pdf查看密码

pdf有两种密码方式,一种是打开后进入文件内容页面后需要密码才能进行修改等操作,网上有很多方式进行移除密码操作,第二种是打开就需要密码,我这里简单记录一个暴力破解的方式,仅供参考

python 复制代码
import PyPDF2
import itertools
import string

def decrypt_pdf(input_file, output_file, password):
    with open(input_file, "rb") as file:
        reader = PyPDF2.PdfReader(file)

        if reader.is_encrypted:
            try:
                if reader.decrypt(password):
                    writer = PyPDF2.PdfWriter()

                    for page in reader.pages:
                        writer.add_page(page)

                    with open(output_file, "wb") as output_pdf_file:
                        writer.write(output_pdf_file)
                        print(f"解密后文件已保存为: {output_file}")
                        return True
                else:
                    print(f"密码'{password}'错误")
            except Exception as e:
                print(f"解密失败: {e}")
        else:
            print("该 PDF 文件没有加密。")
        return False

def generate_passwords(length):
    characters = string.ascii_letters + string.digits  # 字母加数字方式 可以添加其他字符
    for password in itertools.product(characters, repeat=length):
        yield ''.join(password)

if __name__ == "__main__":
    input_pdf_path = r"d:\"  # 输入你的加密 PDF 文件路径
    output_pdf_path = r"d:\"  # 输出解密后文件的路径

    # 尝试生成长度为1到4的所有可能的密码
    for length in range(1, 5):  # 可调整最大长度
        print(f"尝试长度为 {length} 的密码...")
        for password in generate_passwords(length):
            if decrypt_pdf(input_pdf_path, output_pdf_path, password):
                break
相关推荐
桐桐桐1 小时前
PDF 翻译工具横评与自建批量方案:从在线工具到 Python 自动化
python·pdf·自动化
百事牛科技3 小时前
办公文档防护:如何限制 PDF 转换成其他格式文件
windows·pdf
2601_965912834 小时前
520MB卷宗合并压缩踩坑实录:PDF文档归档场景的技术选型与性能对比
pdf
霸道流氓气质4 小时前
本地用 Ollama + 视觉模型识别 PDF 技术教程
pdf
经典19926 小时前
解析pdf表格内容
windows·pdf
星花月8 小时前
【无标题】
ai·语言模型·pdf·deep learning
E_ICEBLUE8 小时前
Python 办公自动化:将 PowerPoint (PPT/PPTX) 转换为 PDF 【全面指南】
python·pdf·powerpoint
XLYcmy1 天前
pdf论文处理:CSV输出模式
数据库·python·pycharm·pdf·论文·csv·dify
SamChan902 天前
用Python+Requests批量翻译PDF:从脚本到调度
后端·python·microsoft·ai·pdf·机器翻译
SamChan903 天前
PDF翻译API的JWT签名鉴权与速率限制设计:安全实战指南
网络·安全·pdf