-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecryt_data.py
48 lines (38 loc) · 1.61 KB
/
decryt_data.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
# ch9_decrypt_blob.py
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
import zlib
class D_Data():
# Our Decryption Function
def decrypt_blob(self,encrypted_blob, private_key):
# Import the Private Key and use for decryption using PKCS1_OAEP
rsakey = RSA.importKey(private_key)
rsakey = PKCS1_OAEP.new(rsakey)
# Base 64 decode the data
encrypted_blob = base64.b64decode(encrypted_blob)
# In determining the chunk size, determine the private key length used in bytes.
# The data will be in decrypted in chunks
chunk_size = 512
offset = 0
decrypted = b""
# keep loop going as long as we have chunks to decrypt
while offset < len(encrypted_blob):
# The chunk
chunk = encrypted_blob[offset: offset + chunk_size]
# Append the decrypted chunk to the overall decrypted file
decrypted += rsakey.decrypt(chunk)
# Increase the offset by chunk size
offset += chunk_size
# return the decompressed decrypted data
return zlib.decompress(decrypted)
def do_the_job(self,filename):
# Use the private key for decryption
with open(".keys/private_key.pem", "rb") as fd:
private_key = fd.read()
# Our candidate file to be decrypted
with open(filename, "rb") as fd:
encrypted_blob = fd.read()
# Write the decrypted contents to a file
with open("decrypted_{}".format(filename), "wb") as fd:
fd.write(self.decrypt_blob(encrypted_blob, private_key))