0
I tested the following script in Python, using Python 2.7.9, with some adaptations made by me, available at https://stackoverflow.com/questions/23900280/how-can-i-encrypt-docx-files-with-aes-pycrypto-without-corrupting-the-files:
# Cifra e decifra documentos nos formatos .pdf, .docx e .rtf
# Adaptacao de Marcelo Ferreira Zochio
from Crypto import Random
from Crypto.Cipher import AES
import hashlib
def pad(s):
padding_size = AES.block_size - len(s) % AES.block_size
return s + b"\0" * padding_size, padding_size
def encrypt(message, key, key_size=256):
message, padding_size = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
enc_bytes = iv + cipher.encrypt(message) + bytes([padding_size])
return enc_bytes
def decrypt(ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CFB, iv)
plaintext = cipher.decrypt(ciphertext)
return plaintext
def encrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = encrypt(plaintext, key)
with open(file_name + ".crp", 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, key)
with open('decifrado_' + file_name, 'wb') as fo:
fo.write(dec)
key = 'chave'
hash_object = hashlib.md5(key.encode())
while True:
filename = raw_input("Arquivo a ser trabalhado: ")
en_de = raw_input("En (cifrar) ou De (decifrar)?")
if en_de.upper() == 'EN':
encrypt_file(filename, hash_object.hexdigest())
elif en_de.upper() == 'DE':
decrypt_file(filename, hash_object.hexdigest())
else:
print("Escolher en ou de!")
cont = raw_input("Continuar?")
if cont.upper() == 'N':
break
It works perfectly, however, when opening documents in . docx and . odt decrypted (deleting the .crp extension and leaving the original) Windows warns that the document is corrupted, and if I wish to recover that document; choosing yes, it recovers it normally and then just save it.
This does not happen with . pdf or .txt. It has something to do with Word or Open Office character formatting?
To make sure your program is encrypting and decrypting files correctly, I suggest you calculate the hash of the file before encryption and after decryption, if the hashes are not equal, surely the file has been corrupted. On Windows, you can use the Winmd5 to obtain the hashes.
– Lacobus