Encryption And Decryption Of Files(part 1)

HOW TO CREATE A PASSWORD PROTECTED PDF DOCUMENTS

You password protected any PDF document with my app,this is to prevent unauthorized access to the PDF file such as your Monthly Bank account statement, this is why Banks do send password protected documents to their customers .

STEPS TO MAKE THE APP ARE AS FOLLOWS:

STEP 1: CREATE A FOLDER ON YOUR COMPUTER WITH THE NAME -PWD OR WHATEVER NAME YOU WANT.TO DO THIS, OPEN YOUR CMD COMMAND LINE.

TYPE mkdir pwd

STEP 2: CREATE A FILE WITH A NAME app.py USING A VSCODE TEXT EDITOR. .COPY THE CODE BELOW AND PASTE THE CODE IN THE app.py and save .

STEP 3.you must install python CORRECTLY on your PC.

STEP 4.you must install pypdf2.

install pypdf2 with the command -pip install PyPDF2

TO RUN THE CODE.

1.Open your CMD Terminal .

2.type cd pwd

3.type python app.py

4.you shall see a pop up

5.type and save the new name of the pdf file you want to encrypt

6.Set and save the password for the new pdf file.

For full explanation ,please watch the video below

import PyPDF2
from tkinter import Tk, filedialog, simpledialog, messagebox

def password_protect_pdf(input_pdf_path, output_pdf_path, password):
    # Open the input PDF file
    with open(input_pdf_path, 'rb') as input_pdf_file:
        pdf_reader = PyPDF2.PdfReader(input_pdf_file)

        # Create a PDF writer object
        pdf_writer = PyPDF2.PdfWriter()

        # Add all pages to the writer
        for page_num in range(len(pdf_reader.pages)):
            pdf_writer.add_page(pdf_reader.pages[page_num])

        # Encrypt the PDF with the password
        pdf_writer.encrypt(password)

        # Write the output PDF file
        with open(output_pdf_path, 'wb') as output_pdf_file:
            pdf_writer.write(output_pdf_file)

# Create a Tkinter root window (it will be hidden)
root = Tk()
root.withdraw()

# Ask the user to select the input PDF file
input_pdf_path = filedialog.askopenfilename(title="Select PDF file to password protect", filetypes=[("PDF files", "*.pdf")])

if input_pdf_path:
    # Ask the user for the output PDF file path
    output_pdf_path = filedialog.asksaveasfilename(title="Save password protected PDF as", defaultextension=".pdf", filetypes=[("PDF files", "*.pdf")])

    if output_pdf_path:
        # Ask the user for the password
        password = simpledialog.askstring("Password", "Enter password to protect the PDF:", show='*')

        if password:
            # Protect the PDF with the password
            password_protect_pdf(input_pdf_path, output_pdf_path, password)
            messagebox.showinfo("Success", "The PDF has been password protected successfully!")
        else:
            messagebox.showwarning("Warning", "No password entered. Operation cancelled.")
    else:
        messagebox.showwarning("Warning", "No output file selected. Operation cancelled.")
else:
    messagebox.showwarning("Warning", "No input file selected. Operation cancelled.")
0
Subscribe to my newsletter

Read articles from RAPHAEL DAPO OLATUNJI directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RAPHAEL DAPO OLATUNJI
RAPHAEL DAPO OLATUNJI