使用pikepdf破解加密PDF时遇RuntimeError: unable to load openssl legacy provider错误
解决pikepdf打开加密PDF时的
RuntimeError: unable to load openssl legacy provider错误 这个错误是因为pikepdf依赖的OpenSSL环境未正确加载legacy加密算法提供者,通常出现在OpenSSL 3.x版本环境中,以下是具体解决方法:
1. 调整OpenSSL环境变量
运行脚本前先清空或调整OpenSSL相关环境变量,强制使用兼容模式:
- Windows(CMD):
set OPENSSL_CONF= set OPENSSL_MODULES= - Linux/macOS(终端):
unset OPENSSL_CONF unset OPENSSL_MODULES
设置完成后再运行你的PDF破解脚本。
2. 重新安装兼容版本的pikepdf与OpenSSL
如果环境变量调整无效,尝试重装依赖,确保版本兼容:
pip uninstall -y pikepdf pip install --upgrade pikepdf
若系统默认OpenSSL是3.x版本,可通过conda安装兼容的1.1版本:
conda install -c conda-forge pikepdf openssl=1.1
3. 优化代码的异常捕获(推荐)
你当前的except:会捕获所有异常,不利于排查问题,建议改为只捕获密码错误和目标运行时错误:
from pikepdf import Pdf, PasswordError import sys from pathlib import Path from random import choice NUMBERS = "0123456789" def crack(self, pdf_file="", numbers_only="n", possible_length=10, **kwargs): file_exist = Path(pdf_file).exists() cracking = True if file_exist: if numbers_only.lower() == "y": while cracking: password = "".join([choice(NUMBERS) for _ in range(possible_length)]) print(f"[SYSTEM] Cracking {pdf_file}: {password}", end="\r") try: Pdf.open(pdf_file, password=password) print("\nPASSWORD FOUND: " + password) cracking = False except PasswordError: continue except RuntimeError as e: print(f"\n[ERROR] 运行时错误: {e}") cracking = False sys.exit(1) else: print(f"[ERROR] File does not exists {pdf_file}") sys.exit(1)
内容的提问来源于stack exchange,提问作者Michael Rodriguez
相关产品推荐
相关产品推荐

