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
相关推荐
乘风!10 小时前
前端Jquery,后端Java实现预览Word、Excel、PPT,pdf等文档
pdf·word·excel·jquery
我有一棵树10 小时前
浏览器使用 <embed> 标签预览 PDF 的原理
pdf·embed
蜀中廖化16 小时前
小技巧:ipynb转pdf
pdf·小工具·python to pdf
Eiceblue18 小时前
使用 Python 向 PDF 添加附件与附件注释
linux·开发语言·vscode·python·pdf
蛋王派19 小时前
本地部署DeepSeek-OCR:打造高效的PDF文字识别服务
人工智能·自然语言处理·pdf·ocr
Iloveskr20 小时前
markdown转为pdf导出
java·pdf
Source.Liu1 天前
【PDF】PDF文件体详解
pdf
非凡ghost1 天前
批量转双层PDF(可识别各种语言) 中文绿色版
前端·windows·pdf·计算机外设·软件需求
夏之繁花1 天前
PDFelement 万兴PDF,简单好用的PDF阅读编辑批注器
pdf
打小就很皮...1 天前
PDF 下载弹窗 content 区域可行性方案
前端·javascript·pdf