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
import json
from lib import symmetric_encryption
2024-11-12 10:26:56 +00:00
logging.basicConfig(format='%(levelname)s\t- %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
BASE_DIR= os.path.join(os.path.expanduser('~'), '.sio/')
2024-11-12 10:26:56 +00:00
#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(BASE_DIR + 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(BASE_DIR + args.metadata)):
logger.error("File '" + args.metadata + "' not found.")
2024-11-12 10:26:56 +00:00
sys.exit(-1)
#Decrypt file
metadata = json.loads(BASE_DIR + args.metadata)
salt = metadata['salt']
2024-11-12 10:26:56 +00:00
content = symmetric_encryption.decrypt_file(metadata['password'], args.encrypted)
2024-11-12 10:26:56 +00:00
# Send decrypted content to stdout
sys.stdout.write(content)
if __name__ == '__main__':
decryptFile(sys.argv[1:])