Skip to content

Instantly share code, notes, and snippets.

@deepal
Created October 28, 2015 22:32
Show Gist options
  • Save deepal/ee6e683e719c209753d8 to your computer and use it in GitHub Desktop.
Save deepal/ee6e683e719c209753d8 to your computer and use it in GitHub Desktop.
def decrypt(filepath):
print "[!] Starting decryption...."
dec_filename = ANONYMOUS_FILEPATH + os.path.basename(filepath).strip(".enc")
inFile = open(filepath,"r")
chunksize=64*1024
hash = inFile.read(32)
encAESKey = inFile.read(512)
private_key_loc = PRIV_KEY_LOC
privkey = open(private_key_loc, "r").read()
rsakey = RSA.importKey(privkey)
rsakey = PKCS1_OAEP.new(rsakey)
aes_key = rsakey.decrypt(encAESKey)
origsize = struct.unpack('<Q', inFile.read(struct.calcsize('Q')))[0]
iv = inFile.read(16)
decryptor = AES.new(aes_key, AES.MODE_CBC, iv)
with open(dec_filename, 'wb') as outfile:
while True:
chunk = inFile.read(chunksize)
if len(chunk) == 0:
break
outfile.write(decryptor.decrypt(chunk))
outfile.truncate(origsize)
print "[+] File was decrypted and saved at \""+dec_filename+"\""
print "[!] Validating integrity..."
if (validateIntegrity(hash, dec_filename)):
print "[+] Integrity validation Passed!"
else:
print "[-] Integrity validation Failed!"
return dec_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment