I’m using a 3DES encryption from this blog (under header Applications) in python with Crypto, And I’ve been testing it on the Tree of Fun Files, as an example for this question the An Accountant and his Frog.txt among many other files (file size varies greatly, for example TOASTERS).
pyencrypt
import os
from Crypto.Cipher import DES3
def encrypt_file(in_filename, out_filename, chunk_size, key, iv):
des3 = DES3.new(key, DES3.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
out_file.write(des3.encrypt(chunk))
def decrypt_file(in_filename, out_filename, chunk_size, key, iv):
des3 = DES3.new(key, DES3.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
out_file.write(des3.decrypt(chunk))
Usage
import pyencrypt, md5
from Crypto import Random
iv = Random.get_random_bytes(8)
m = md5.new()
m.update("encryptionkey")
key = m.digest()
.encrypt_file("C:\\treeoffunfiles\\Accountant and his frog.txt", 'C:\\treeoffun\\to_enc.enc', 8192, key, iv)
pyencrypt.decrypt_file('C:\\treeoffunfiles\\to_enc.enc', 'C:\\treeoffunfiles\\to_enc.dec', 8192, key, iv)
And the encryption of files is great (no complaints there)! but the decryption of the file isn’t as great. Here is an comparison output between the original file and the decrypted file from notepad++.

(source: iforce.co.nz)
Any ideas on why the decrypted file is missing content from the original? and how can I make the decryption (and encryption if necessary) more accurate (across files that may vary in size)?
The suggested answer was posted in the comments by DSM (I’ve been waiting for him to post answer, this can stay for the meantime).
The problem was that the original implementation of the 3DES encryption was ran on a
unixmachine. Whereas the results on awindowssystem differ.In order to fix the
chunksof missing data, DSM suggested to change the file input/output to binary instead of regular reading/writting, which has given the desired output as seen here.fyi diffnow only stores the results for 1 month.