I tested the following script in Python, using Python 2.7.9, with some adaptations made by me, available at link :
# 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"# 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"%pre%" * 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
" * 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 decrypted .docx and .odt documents (erasing the .crp extension and leaving the original) Windows warns that the document is corrupted, and if I want to recover that document; choosing yes, he recovers it normally and then just save it.
This does not happen with .pdf or .txt. Is there anything to do with Word or Open Office character formatting?