@'
from pathlib import Path
from pypdf import PdfReader, PdfWriter
import os
folder = Path(os.environ["USERPROFILE"]) / "Desktop" / "LR4"
source = folder / "LR4_PDF_encrypted.pdf"
target = folder / "LR4_FINAL_PROTECTED.pdf"
password = "P7d!"
if not source.exists():
raise FileNotFoundError(source)
reader = PdfReader(str(source))
if reader.is_encrypted:
raise RuntimeError("Source already encrypted")
writer = PdfWriter(clone_from=str(source))
writer.encrypt(
user_password=password,
owner_password="OwnerPassword2026",
algorithm="AES-128"
)
writer.write(str(target))
# Independent verification
test = PdfReader(str(target))
is_encrypted = test.is_encrypted
empty_result = test.decrypt("")
correct_result = PdfReader(str(target)).decrypt(password)
print()
print("=== PDF VERIFICATION ===")
print("File:", target)
print("Encrypted:", is_encrypted)
print("Empty password accepted:", empty_result != 0)
print("Correct password accepted:", correct_result != 0)
if is_encrypted and empty_result == 0 and correct_result != 0:
print("SUCCESS: PDF IS PROTECTED!")
else:
raise RuntimeError("Protection verification FAILED")
'@ | py -