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)