From 2ad74dfcad14288235e069f5404c837c467c62b5 Mon Sep 17 00:00:00 2001 From: brunombpereira Date: Tue, 16 May 2023 18:30:34 +0100 Subject: [PATCH] [LABI] tema01: added cipher_ARC4.py --- 1ano/2semestre/labi/tema01/src/cipher_ARC4.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 1ano/2semestre/labi/tema01/src/cipher_ARC4.py diff --git a/1ano/2semestre/labi/tema01/src/cipher_ARC4.py b/1ano/2semestre/labi/tema01/src/cipher_ARC4.py new file mode 100644 index 0000000..c4df5d4 --- /dev/null +++ b/1ano/2semestre/labi/tema01/src/cipher_ARC4.py @@ -0,0 +1,32 @@ +import sys +import hashlib +from Crypto.Cipher import ARC4 + + +def generate_key(key): + if len(key) < 5: + # Gerar uma síntese de chave com SHA-256 + key = hashlib.sha256(key.encode('utf-8')).digest() + elif len(key) > 256: + # Usar apenas os primeiros 256 octetos + key = key[:256].encode('utf-8') + else: + key = key.encode('utf-8') + return key + + +def encrypt_file(file_path, key): + with open(file_path, 'rb') as file: + data = file.read() + cipher = ARC4.new(generate_key(key)) + ciphertext = cipher.encrypt(data) + sys.stdout.buffer.write(ciphertext) + + +if __name__ == '__main__': + if len(sys.argv) != 3: + print('Usage: python3 Ex4.py file key > file_to_send_encriptation', file=sys.stderr) + sys.exit(1) + file_path = sys.argv[1] + key = sys.argv[2] + encrypt_file(file_path, key)