-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_protect.py
More file actions
205 lines (163 loc) · 5.29 KB
/
Copy pathexample_protect.py
File metadata and controls
205 lines (163 loc) · 5.29 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import aspose.pdf as ap
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
def encrypt_password(infile, outfile):
"""
Encrypt PDF with password and permissions.
Args:
infile (str): Input PDF filename
outfile (str): Output PDF filename
Returns:
None
Example:
encrypt_password("sample.pdf", "sample_protected.pdf")
Note:
Uses AES 128-bit encryption. Allows screen readers, forbids all other operations.
User password: "userpassword", Owner password: "ownerpassword".
"""
document = ap.Document(infile)
document_privilege = ap.facades.DocumentPrivilege.forbid_all
document_privilege.allow_screen_readers = True
document.encrypt(
"userpassword",
"ownerpassword",
document_privilege,
ap.CryptoAlgorithm.AESx128,
False,
)
document.save(outfile)
def encrypt_pdf_file(infile, outfile):
"""
Encrypt PDF with full permissions.
Args:
infile (str): Input PDF filename
outfile (str): Output PDF filename
Returns:
None
Example:
encrypt_pdf_file("sample.pdf", "sample_protected.pdf")
Note:
Uses RC4 128-bit encryption with allow_all privileges.
"""
document = ap.Document(infile)
document.encrypt(
"userpassword",
"ownerpassword",
ap.facades.DocumentPrivilege.allow_all,
ap.CryptoAlgorithm.RC4x128,
False,
)
document.save(outfile)
def decrypt_pdf_file(infile, outfile):
"""
Decrypt password-protected PDF.
Args:
infile (str): Input encrypted PDF filename
outfile (str): Output decrypted PDF filename
Returns:
None
Example:
decrypt_pdf_file("sample_protected.pdf", "sample_unprotected.pdf")
Note:
Requires user password to open document.
"""
document = ap.Document(infile, "userpassword")
document.decrypt()
document.save(outfile)
def change_password(infile, outfile):
"""
Change user and owner passwords.
Args:
infile (str): Input PDF filename
outfile (str): Output PDF filename
Returns:
None
Example:
change_password("sample_protected.pdf", "sample_changepassword.pdf")
Note:
Changes from ownerpassword to newuser/newowner.
"""
document = ap.Document(infile, "ownerpassword")
document.change_passwords("ownerpassword", "newuser", "newowner")
document.save(outfile)
def determine_correct_password_from_list(infile):
"""
Test multiple passwords to find correct one.
Args:
infile (str): Input encrypted PDF filename
Returns:
None
Example:
determine_correct_password_from_list("sample_protected.pdf")
Note:
Tries passwords: test, test1, test2, test3, userpassword.
Prints page count if password is correct.
"""
info = ap.facades.PdfFileInfo(infile)
print(f"File is password protected {info.is_encrypted}")
passwords = "test", "test1", "test2", "test3", "userpassword"
for password in passwords:
try:
document = ap.Document(infile, password)
count = len(document.pages)
if count > 0:
print(f"Password '{password}' is correct. Number of pages in document: {count}")
except RuntimeError as ex:
if "InvalidPasswordException" in str(type(ex)) or "InvalidPasswordException" in str(ex):
print(f"Wrong password: {password}")
else:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print(message)
def run_all_examples(data_dir=None, license_path=None):
"""Run Protect Document examples and report status.
Args:
data_dir (str, optional): Input/output directory override.
license_path (str, optional): Path to Aspose.PDF license file.
Returns:
None
"""
set_license(license_path)
input_dir, output_dir = initialize_data_dir(data_dir)
examples = [
(
"Encrypt with password",
encrypt_password,
"sample3.pdf",
"sample_protected.pdf",
),
("Encrypt PDF file", encrypt_pdf_file, "sample3.pdf", "sample_protected.pdf"),
(
"Change password",
change_password,
"sample_protected.pdf",
"sample_changepassword.pdf",
),
(
"Decrypt PDF",
decrypt_pdf_file,
"sample_protected.pdf",
"sample_unprotected.pdf",
),
(
"Determine password",
determine_correct_password_from_list,
"sample_protected.pdf",
None,
),
]
for name, func, infile, outfile in examples:
try:
input_file = path.join(input_dir, infile)
if outfile:
output_file = path.join(output_dir, outfile)
func(input_file, output_file)
else:
func(input_file)
print(f"✅ Success: {name}")
except Exception as e:
print(f"❌ Failed: {name} - {e}")
if __name__ == "__main__":
run_all_examples()