forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt_pdf.py
190 lines (168 loc) · 7.02 KB
/
encrypt_pdf.py
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
# Import Libraries
from PyPDF4 import PdfFileReader, PdfFileWriter, utils
import os
import argparse
import getpass
from io import BytesIO
import pyAesCrypt
# Size of chunck
BUFFER_SIZE = 64*1024
def is_encrypted(input_file: str) -> bool:
"""Checks if the inputted file is encrypted using PyPDF4 library"""
with open(input_file, 'rb') as pdf_file:
pdf_reader = PdfFileReader(pdf_file, strict=False)
return pdf_reader.isEncrypted
def encrypt_pdf(input_file: str, password: str):
"""
Encrypts a file using PyPDF4 library.
Precondition: File is not encrypted.
"""
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(open(input_file, 'rb'), strict=False)
if pdf_reader.isEncrypted:
print(f"PDF File {input_file} already encrypted")
return False, None, None
try:
# To encrypt all the pages of the input file, you need to loop over all of them
# and to add them to the writer.
for page_number in range(pdf_reader.numPages):
pdf_writer.addPage(pdf_reader.getPage(page_number))
except utils.PdfReadError as e:
print(f"Error reading PDF File {input_file} = {e}")
return False, None, None
# The default is 128 bit encryption (if false then 40 bit encryption).
pdf_writer.encrypt(user_pwd=password, owner_pwd=None, use_128bit=True)
return True, pdf_reader, pdf_writer
def decrypt_pdf(input_file: str, password: str):
"""
Decrypts a file using PyPDF4 library.
Precondition: A file is already encrypted
"""
pdf_reader = PdfFileReader(open(input_file, 'rb'), strict=False)
if not pdf_reader.isEncrypted:
print(f"PDF File {input_file} not encrypted")
return False, None, None
pdf_reader.decrypt(password=password)
pdf_writer = PdfFileWriter()
try:
for page_number in range(pdf_reader.numPages):
pdf_writer.addPage(pdf_reader.getPage(page_number))
except utils.PdfReadError as e:
print(f"Error reading PDF File {input_file} = {e}")
return False, None, None
return True, pdf_reader, pdf_writer
def cipher_stream(inp_buffer: BytesIO, password: str):
"""Ciphers an input memory buffer and returns a ciphered output memory buffer"""
# Initialize output ciphered binary stream
out_buffer = BytesIO()
inp_buffer.seek(0)
# Encrypt Stream
pyAesCrypt.encryptStream(inp_buffer, out_buffer, password, BUFFER_SIZE)
out_buffer.seek(0)
return out_buffer
def decipher_file(input_file: str, output_file: str, password: str):
"""
Deciphers an input file and returns a deciphered output file
"""
inpFileSize = os.stat(input_file).st_size
out_buffer = BytesIO()
with open(input_file, mode='rb') as inp_buffer:
try:
# Decrypt Stream
pyAesCrypt.decryptStream(
inp_buffer, out_buffer, password, BUFFER_SIZE, inpFileSize)
except Exception as e:
print("Exception", str(e))
return False
inp_buffer.close()
if out_buffer:
with open(output_file, mode='wb') as f:
f.write(out_buffer.getbuffer())
f.close()
return True
def encrypt_decrypt_file(**kwargs):
"""Encrypts or decrypts a file"""
input_file = kwargs.get('input_file')
password = kwargs.get('password')
output_file = kwargs.get('output_file')
action = kwargs.get('action')
# Protection Level
# Level 1 --> Encryption / Decryption using PyPDF4
# Level 2 --> Encryption and Ciphering / Deciphering and Decryption
level = kwargs.get('level')
if not output_file:
output_file = input_file
if action == "encrypt":
result, pdf_reader, pdf_writer = encrypt_pdf(
input_file=input_file, password=password)
# Encryption completed successfully
if result:
output_buffer = BytesIO()
pdf_writer.write(output_buffer)
pdf_reader.stream.close()
if level == 2:
output_buffer = cipher_stream(output_buffer, password=password)
with open(output_file, mode='wb') as f:
f.write(output_buffer.getbuffer())
f.close()
elif action == "decrypt":
if level == 2:
decipher_file(input_file=input_file,
output_file=output_file, password=password)
result, pdf_reader, pdf_writer = decrypt_pdf(
input_file=input_file, password=password)
# Decryption completed successfully
if result:
output_buffer = BytesIO()
pdf_writer.write(output_buffer)
pdf_reader.stream.close()
with open(output_file, mode='wb') as f:
f.write(output_buffer.getbuffer())
f.close()
class Password(argparse.Action):
"""
Hides the password entry
"""
def __call__(self, parser, namespace, values, option_string):
if values is None:
values = getpass.getpass()
setattr(namespace, self.dest, values)
def is_valid_path(path):
"""Validates the path inputted and checks whether it is a file path or a folder path"""
if not path:
raise ValueError(f"Invalid Path")
if os.path.isfile(path):
return path
elif os.path.isdir(path):
return path
else:
raise ValueError(f"Invalid Path {path}")
def parse_args():
"""Get user command line parameters"""
parser = argparse.ArgumentParser(description="These options are available")
parser.add_argument("file", help="Input PDF file you want to encrypt", type=is_valid_path)
# parser.add_argument('-i', '--input_path', dest='input_path', type=is_valid_path,
# required=True, help="Enter the path of the file or the folder to process")
parser.add_argument('-a', '--action', dest='action', choices=[
'encrypt', 'decrypt'], type=str, default='encrypt', help="Choose whether to encrypt or to decrypt")
parser.add_argument('-l', '--level', dest='level', choices=[
1, 2], type=int, default=1, help="Choose which protection level to apply")
parser.add_argument('-p', '--password', dest='password', action=Password,
nargs='?', type=str, required=True, help="Enter a valid password")
parser.add_argument('-o', '--output_file', dest='output_file',
type=str, help="Enter a valid output file")
args = vars(parser.parse_args())
# To Display Command Arguments Except Password
print("## Command Arguments #################################################")
print("\n".join("{}:{}".format(i, j)
for i, j in args.items() if i != 'password'))
print("######################################################################")
return args
if __name__ == '__main__':
# Parsing command line arguments entered by user
args = parse_args()
# Encrypting or Decrypting File
encrypt_decrypt_file(
input_file=args['file'], password=args['password'],
action=args['action'], level=args['level'], output_file=args['output_file']
)