from Crypto.Cipher import DES from secrets import token_bytes import sys import datetime def encrypt(message): cipher = DES.new(key, DES.MODE_EAX) nonce = cipher.nonce ciphertext, tag = cipher.encrypt_and_digest(message.encode('ascii')) return nonce, ciphertext, tag def decrypt(nonce, ciphertext, tag): cipher = DES.new(key, DES.MODE_EAX, nonce=nonce) plaintext = cipher.decrypt(ciphertext) try: cipher.verify(tag) return plaintext.decoce('ascii') except: return False key=token_bytes(8) print('Zahajuji čtení souboru.') start_time = datetime.datetime.now() filename = str(sys.argv[1]) file = open(filename) lines = file.readlines() file.close() text = '' for line in lines: text += str(line) filereaddelta = datetime.datetime.now() - start_time print('Čtení souboru úspěšné.') n = 5000000 chunks = [text[i:i+n] for i in range(0, len(text), n)] ciphertext = bytes(''.encode('ascii')) encryptdelta = int(filereaddelta.total_seconds() * 1000) decryptdelta = 0 filename = filename.replace(".", "_encrypted.") filewrite = open(filename, 'w') fileread = open(filename, 'r') for chunk in chunks: start_time = datetime.datetime.now() nonce, ciphertext, tag = encrypt(chunk) filewrite.write(str(ciphertext)) delta = datetime.datetime.now() - start_time encryptdelta += int(delta.total_seconds() * 1000) start_time = datetime.datetime.now() text = fileread.readlines() plaintext = decrypt(nonce, ciphertext, tag) delta = datetime.datetime.now() - start_time decryptdelta += int(delta.total_seconds() * 1000) filewrite.close() fileread.close() print('Šifrování trvalo: %s ms' % encryptdelta) print('Rozšifrování trvalo: %s ms' % decryptdelta)