Q1 e Q2 acabadas

Signed-off-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
Tiago Sousa 2024-02-20 19:31:56 +00:00
parent 1227017b76
commit 8d679cab60
4 changed files with 61 additions and 20 deletions

View file

@ -3,6 +3,7 @@
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
import sys
import os
import argparse
def setup(key_file):
f = open(key_file,"wb")
@ -31,7 +32,7 @@ def encrypt(input_file, key_file):
def decrypt(input_file,key_file):
fkey = open(key_file,"rb")
inp = open(f"{input_file}.enc","rb")
inp = open(f"{input_file}","rb")
out = open(f"{input_file}.dec","wb")
key = fkey.read()
@ -50,33 +51,41 @@ def decrypt(input_file,key_file):
out.close()
def main():
args = sys.argv[1:]
if len(args) == 0:
sys.exit("Needs at least 2 arguments, use help.")
parser = argparse.ArgumentParser(
description="Program to perform operations using ChaCha20 cipher on files",
)
match args[0]:
subparsers = parser.add_subparsers(dest="operation", help="Operation to perform")
# Setup subcommand
setup_parser = subparsers.add_parser("setup", help="Setup a key file")
setup_parser.add_argument("fkey", help="File to contain the appropriate key for the ChaCha20 cipher")
# Encrypt subcommand
enc_parser = subparsers.add_parser("enc", help="Encrypt a file")
enc_parser.add_argument("fich", help="File to be encrypted")
enc_parser.add_argument("fkey", help="File containing the key for the ChaCha20 cipher")
# Decrypt subcommand
dec_parser = subparsers.add_parser("dec", help="Decrypt a file")
dec_parser.add_argument("fich", help="File to be decrypted")
dec_parser.add_argument("fkey", help="File containing the key for the ChaCha20 cipher")
args = parser.parse_args()
match args.operation:
case "setup":
if len(args) != 2:
print("Number of arguments is incorrect for setup option")
key_file = args[1]
key_file = args.fkey
setup(key_file)
case "enc":
if len(args) != 3:
print("Number of arguments is incorrect for enc option")
input_file = args[1]
key_file = args[2]
input_file = args.fich
key_file = args.fkey
encrypt(input_file,key_file)
case "dec":
if len(args) != 3:
print("Number of arguments is incorrect for dec option")
input_file = args[1]
key_file = args[2]
input_file = args.fich
key_file = args.fkey
decrypt(input_file,key_file)
case "help":
print("skill issue")
case _:
print(f"{args[1]} is not an option, use help")
parser.print_help()
if __name__ == "__main__":
main()