Q1 e Q2 acabadas
Signed-off-by: tsousa111 <tiagao2001@hotmail.com>
This commit is contained in:
parent
1227017b76
commit
8d679cab60
4 changed files with 61 additions and 20 deletions
|
@ -3,6 +3,7 @@
|
||||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
def setup(key_file):
|
def setup(key_file):
|
||||||
f = open(key_file,"wb")
|
f = open(key_file,"wb")
|
||||||
|
@ -31,7 +32,7 @@ def encrypt(input_file, key_file):
|
||||||
|
|
||||||
def decrypt(input_file,key_file):
|
def decrypt(input_file,key_file):
|
||||||
fkey = open(key_file,"rb")
|
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")
|
out = open(f"{input_file}.dec","wb")
|
||||||
|
|
||||||
key = fkey.read()
|
key = fkey.read()
|
||||||
|
@ -50,33 +51,41 @@ def decrypt(input_file,key_file):
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
args = sys.argv[1:]
|
parser = argparse.ArgumentParser(
|
||||||
if len(args) == 0:
|
description="Program to perform operations using ChaCha20 cipher on files",
|
||||||
sys.exit("Needs at least 2 arguments, use help.")
|
)
|
||||||
|
|
||||||
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":
|
case "setup":
|
||||||
if len(args) != 2:
|
key_file = args.fkey
|
||||||
print("Number of arguments is incorrect for setup option")
|
|
||||||
key_file = args[1]
|
|
||||||
setup(key_file)
|
setup(key_file)
|
||||||
case "enc":
|
case "enc":
|
||||||
if len(args) != 3:
|
input_file = args.fich
|
||||||
print("Number of arguments is incorrect for enc option")
|
key_file = args.fkey
|
||||||
input_file = args[1]
|
|
||||||
key_file = args[2]
|
|
||||||
encrypt(input_file,key_file)
|
encrypt(input_file,key_file)
|
||||||
case "dec":
|
case "dec":
|
||||||
if len(args) != 3:
|
input_file = args.fich
|
||||||
print("Number of arguments is incorrect for dec option")
|
key_file = args.fkey
|
||||||
input_file = args[1]
|
|
||||||
key_file = args[2]
|
|
||||||
decrypt(input_file,key_file)
|
decrypt(input_file,key_file)
|
||||||
|
|
||||||
case "help":
|
case "help":
|
||||||
print("skill issue")
|
parser.print_help()
|
||||||
case _:
|
|
||||||
print(f"{args[1]} is not an option, use help")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
30
TPs/TP02/py/chacha20_int_attck.py
Executable file
30
TPs/TP02/py/chacha20_int_attck.py
Executable file
|
@ -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 <fctxt> <pos> <ptxtAtPos> <newPtxtAtPos>")
|
||||||
|
|
||||||
|
attack(argv[0],int(argv[1]),argv[2],argv[3])
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
1
TPs/TP02/py/input.txt.enc.attck
Normal file
1
TPs/TP02/py/input.txt.enc.attck
Normal file
|
@ -0,0 +1 @@
|
||||||
|
…[nÍ"[v©Õ·µ2SÈ.[n{Vƒ#<23>ÿì<C3BF>»
|
1
TPs/TP02/py/input.txt.enc.attck.dec
Normal file
1
TPs/TP02/py/input.txt.enc.attck.dec
Normal file
|
@ -0,0 +1 @@
|
||||||
|
vegan é fixe!!!
|
Loading…
Reference in a new issue