PDF encryption is commonly used to restrict editing, copying, or printing. In many enterprise workflows—legal documentation, invoice processing, automation pipelines—you may need to remove protection from a PDF file programmatically, provided you have the authorized password.
In this guide, you’ll learn how to:
- Detect whether a PDF is encrypted
- Ask the user for file input dynamically
- Prompt securely for a password
- Generate an unprotected copy
- Handle errors cleanly
- We will use the Python library pypdf, the maintained successor to PyPDF2.
Understanding PDF Protection Types
Before writing code, understand what “protected” means:
NOTE - ⚠ Always ensure you have legal rights to modify the document.
Step 1 — Install Required Library
pip install pypdf
Step 2 — Python Script (User Input Based)
Below is a production-safe CLI script.
from pypdf import PdfReader, PdfWriter
import os
import getpass
def unlock_pdf():
# Ask user for input file
input_path = input("Enter PDF file path: ").strip()
if not os.path.exists(input_path):
print("❌ File not found.")
return
# Output file name
output_path = input("Enter output PDF file name (e.g., unlocked.pdf): ").strip()
try:
reader = PdfReader(input_path)
# Check encryption
if reader.is_encrypted:
print("🔐 PDF is encrypted.")
# Ask password securely
password = getpass.getpass("Enter PDF password: ")
if not reader.decrypt(password):
print("❌ Wrong password.")
return
else:
print("✅ Password accepted.")
else:
print("ℹ️ PDF is not encrypted, removing restrictions anyway.")
# Write unlocked file
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
with open(output_path, "wb") as f:
writer.write(f)
print(f"✅ Unlocked PDF saved as: {output_path}")
except Exception as e:
print("❌ Error:", str(e))
if __name__ == "__main__":
unlock_pdf()
Code Breakdown (Technical Explanation)
1️⃣ PdfReader
Loads the PDF file into memory.
2️⃣ is_encrypted
Checks whether the PDF is protected.
3️⃣ decrypt(password)
Attempts decryption using user-provided password.
4️⃣ PdfWriter
Rewrites pages into a new file without restrictions.
Execution Flow
User provides file path
↓
Program checks encryption
↓
If encrypted → Ask password
↓
Decrypt → Copy pages
↓
Write new PDF (unprotected)
Security Considerations
As someone with a security background (especially if you're exploring automation or SecDevOps workflows), keep these points in mind:
- Never store plaintext passwords in logs
- Use getpass() for secure input
- Avoid automating password cracking
- Log only metadata, not document content
- If integrating into CI/CD pipelines:
- Use environment variables for password input
- Restrict file system access
- Validate file extensions
Final Thoughts
Automating PDF decryption in Python is straightforward when using the correct libraries and maintaining proper security practices. This approach is ideal for:
- IT automation workflows
- Enterprise document processing
- Backend document services
- Internal compliance tools
Disclaimer
All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.




