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
相关推荐
daanpdf3 分钟前
考研英语一历年真题及答案解析PDF(2010-2026)百度网盘
考研·pdf
daanpdf2 小时前
历年考研数学一、数学二、数学三真题试卷及答案PDF
考研·pdf
南风微微吹3 小时前
2026年初级社会工作者考试历年真题及答案解析PDF电子版(2010-2025年)
pdf
俊哥工具3 小时前
不用安装不收费!多功能U盘修复工具,解决大部分U盘故障
学习·pdf·word·excel·音视频
daanpdf5 小时前
曹德旺《心若菩提》PDF及读后感
pdf
asdzx6713 小时前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
小婷资料库15 小时前
2025年新高考二卷数学真题试卷及答案解析电子版PDF
pdf·高考
南风微微吹20 小时前
2015-2025年英语六级历年真题及答案解析电子版PDF(含听力音频)
pdf·英语六级
小为资料库1 天前
2026年初级社会工作者考试历年真题及答案解析电子版pdf(2010-2025年)
pdf