sio-2425/delivery1/client/bin/rep_decrypt_file

54 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2024-11-12 10:26:56 +00:00
#!/bin/python3
import os
import sys
import logging
import argparse
import json
sys.path.append(os.path.abspath("../../"))
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)
2024-11-20 22:03:50 +00:00
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()
2024-11-20 20:49:40 +00:00
if not args.encrypted or not args.metadata:
2024-11-12 10:26:56 +00:00
logger.error("Need encrypted file and it's metadata.")
2024-11-20 20:49:40 +00:00
sys.exit(1)
2024-11-12 10:26:56 +00:00
# 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-20 20:49:40 +00:00
sys.exit(1)
2024-11-12 10:26:56 +00:00
# if (not os.path.isfile(BASE_DIR + args.metadata)):
# logger.error("File '" + args.metadata + "' not found.")
# sys.exit(1)
2024-11-12 10:26:56 +00:00
#Decrypt file
print(args.metadata)
metadata = json.loads(args.metadata)
2024-11-12 10:26:56 +00:00
content = symmetric_encryption.decrypt_file(metadata['nonce'].encode(), metadata['key'].encode(), BASE_DIR + args.encrypted)
2024-11-12 10:26:56 +00:00
# Send decrypted content to stdout
sys.stdout.write(content)
2024-11-20 20:49:40 +00:00
sys.exit(0)
2024-11-12 10:26:56 +00:00
if __name__ == '__main__':
decryptFile(sys.argv[1:])