sio-2425/delivery1/client/bin/rep_decrypt_file

52 lines
1.3 KiB
Plaintext
Raw Normal View History

2024-11-12 10:26:56 +00:00
#!/bin/python3
import os
import sys
import logging
import argparse
from lib import decryption_functs
2024-11-12 10:26:56 +00:00
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#send to stdout contents of decrypted file
# encrypted file - encryption metadata
def decryptFile(args):
parser = argparse.ArgumentParser()
parser.add_argument('encrypted', nargs='?', default=None)
parser.add_argument('metadata', nargs='?', default=None)
args = parser.parse_args()
if len(args) != 2:
2024-11-12 10:26:56 +00:00
logger.error("Need encrypted file and it's metadata.")
sys.exit(-1)
# If first argument is not a file or not found
if (not os.path.isfile(args.encrypted)):
logger.error("File '" + args.encrypted + "' not found.")
2024-11-12 10:26:56 +00:00
sys.exit(-1)
if (not os.path.isfile(args.metadata)):
logger.error("File '" + args.metadata + "' not found.")
2024-11-12 10:26:56 +00:00
sys.exit(-1)
#Get private key to decrypt
privateKey = decryption_functs.load_private_key(args.metadata)
2024-11-12 10:26:56 +00:00
#Decrypt file
with open(args.encrypted, 'rb') as f:
content = f.read()
2024-11-12 10:26:56 +00:00
content = decryption_functs.decrypt_file(privateKey, content)
2024-11-12 10:26:56 +00:00
# Send decrypted content to stdout
sys.stdout.write(content)
if __name__ == '__main__':
decryptFile(sys.argv[1:])