50 lines
1.3 KiB
Python
Executable File
50 lines
1.3 KiB
Python
Executable File
#!/bin/python3
|
|
import os
|
|
import sys
|
|
import logging
|
|
import argparse
|
|
import json
|
|
|
|
sys.path.append(os.path.abspath("../../"))
|
|
from lib import symmetric_encryption
|
|
|
|
logging.basicConfig(format='%(levelname)s\t- %(message)s')
|
|
logger = logging.getLogger()
|
|
logger.setLevel(logging.INFO)
|
|
|
|
BASE_DIR = os.path.join(os.path.expanduser('~'), '.sio/')
|
|
|
|
#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 not args.encrypted or not args.metadata:
|
|
logger.error("Need encrypted file and its 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.")
|
|
sys.exit(1)
|
|
|
|
metadata = json.loads(args.metadata)
|
|
|
|
content = symmetric_encryption.decrypt_file(bytes.fromhex(metadata['key']), BASE_DIR + args.encrypted)
|
|
|
|
with open(BASE_DIR + 'decrypted_file.txt', 'w') as f:
|
|
f.write(content)
|
|
|
|
# Send decrypted content to stdout
|
|
sys.stdout.write(content)
|
|
sys.exit(0)
|
|
|
|
if __name__ == '__main__':
|
|
decryptFile(sys.argv[1:])
|