From 8d679cab600ab3dd946856229296739859caf364 Mon Sep 17 00:00:00 2001 From: tsousa111 Date: Tue, 20 Feb 2024 19:31:56 +0000 Subject: [PATCH] Q1 e Q2 acabadas Signed-off-by: tsousa111 --- TPs/TP02/py/cfich_chacha20.py | 49 +++++++++++++++++------------ TPs/TP02/py/chacha20_int_attck.py | 30 ++++++++++++++++++ TPs/TP02/py/input.txt.enc.attck | 1 + TPs/TP02/py/input.txt.enc.attck.dec | 1 + 4 files changed, 61 insertions(+), 20 deletions(-) create mode 100755 TPs/TP02/py/chacha20_int_attck.py create mode 100644 TPs/TP02/py/input.txt.enc.attck create mode 100644 TPs/TP02/py/input.txt.enc.attck.dec diff --git a/TPs/TP02/py/cfich_chacha20.py b/TPs/TP02/py/cfich_chacha20.py index fc233bf..dde5418 100755 --- a/TPs/TP02/py/cfich_chacha20.py +++ b/TPs/TP02/py/cfich_chacha20.py @@ -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() diff --git a/TPs/TP02/py/chacha20_int_attck.py b/TPs/TP02/py/chacha20_int_attck.py new file mode 100755 index 0000000..60ad33c --- /dev/null +++ b/TPs/TP02/py/chacha20_int_attck.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys + +def attack(fctxt, pos, plainAtPos, newPlainAtPos): + f = open(fctxt,"rb") + ciphertext = f.read() + f.close() + + plainAtPos = plainAtPos.encode() + newPlainAtPos = newPlainAtPos.encode() + txt_len = len(plainAtPos) + diff = bytes([a ^ b for (a,b) in zip(plainAtPos,newPlainAtPos)]) + cipher_diff = bytes([a ^ b for (a,b) in zip(diff,ciphertext[pos:pos+txt_len])]) + + new_ciphertext = ciphertext[:pos] + cipher_diff + ciphertext[pos+txt_len:] + + with open(fctxt+".attck","wb") as f: + f.write(new_ciphertext) + +def main(): + argv = sys.argv[1:] + argc = len(argv) + if argc < 3 or argc > 5: + sys.exit("Needs 4 arguments ") + + attack(argv[0],int(argv[1]),argv[2],argv[3]) + +if __name__ == "__main__": + main() diff --git a/TPs/TP02/py/input.txt.enc.attck b/TPs/TP02/py/input.txt.enc.attck new file mode 100644 index 0000000..be9036e --- /dev/null +++ b/TPs/TP02/py/input.txt.enc.attck @@ -0,0 +1 @@ +[n "[vշ2 S.[n{V#쁻 \ No newline at end of file diff --git a/TPs/TP02/py/input.txt.enc.attck.dec b/TPs/TP02/py/input.txt.enc.attck.dec new file mode 100644 index 0000000..4059da1 --- /dev/null +++ b/TPs/TP02/py/input.txt.enc.attck.dec @@ -0,0 +1 @@ +vegan é fixe!!!