File size: 2,221 Bytes
49412e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys
import argparse
from pypdf import PdfReader, PdfWriter

def main():
    parser = argparse.ArgumentParser(description="PDF Security Script")
    parser.add_argument("action", choices=["unlock", "protect"], help="Action to perform")
    parser.add_argument("input", help="Path to input PDF")
    parser.add_argument("output", help="Path to output PDF")
    parser.add_argument("--password", help="Password for decryption/encryption", default="")

    args = parser.parse_args()

    try:
        reader = PdfReader(args.input)
        writer = PdfWriter()

        if args.action == "unlock":
            if reader.is_encrypted:
                # Try empty string first (for Owner locked files)
                decrypted = False
                try:
                    if reader.decrypt(""):
                        decrypted = True
                except Exception:
                    pass
                
                # If explicitly provided password
                if not decrypted and args.password:
                    try:
                        if reader.decrypt(args.password):
                            decrypted = True
                    except Exception:
                        pass
                
                if not decrypted:
                    print("ERROR: Invalid password or unsupported encryption")
                    sys.exit(1)

            # Copy all pages
            for page in reader.pages:
                writer.add_page(page)

            # Write unencrypted output
            with open(args.output, "wb") as f:
                writer.write(f)
            print("SUCCESS")

        elif args.action == "protect":
            # Copy all pages
            for page in reader.pages:
                writer.add_page(page)

            # Encrypt output
            if args.password:
                writer.encrypt(args.password)
            else:
                print("ERROR: Password empty for protection")
                sys.exit(1)

            with open(args.output, "wb") as f:
                writer.write(f)
            print("SUCCESS")

    except Exception as e:
        print(f"ERROR: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main()