treat binary data as binary data
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 30 Jan 2020 15:36:14 +0000 (16:36 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sat, 1 Feb 2020 14:10:00 +0000 (15:10 +0100)
The script ``file_crypt.py'' was opening both the source and the
sink in ``text mode'' which already messes with the input, then
encoding the result as UTF-8 encoded strings before writing
it back. Needless to say this was corrupting data all over the
place as soon as the payload contained non-ASCII bytes.

file_crypt.py

index 9a91768..dd74bf5 100755 (executable)
@@ -43,7 +43,7 @@ def main(do_encrypt, in_file, out_file, password, comptype='gz',
     try:
         # open file to read
         if do_encrypt:
-            read_handle = open(in_file, 'rt')
+            read_handle = open(in_file, 'rb')
         else:
             decryptor = initialize_encryption (CRYPTO_MODE_DECRYPT,
                                                password=password)
@@ -60,17 +60,20 @@ def main(do_encrypt, in_file, out_file, password, comptype='gz',
                                            comptype=comptype, bufsize=bufsize,
                                            fileobj=None, encryption=encryptor)
         else:
-            write_handle = open(out_file, 'wt')
+            write_handle = open(out_file, 'wb')
         return_code = 1
 
         # convert
+        total = 0
         while True:
             buf = read_handle.read(bufsize)
+            total += len(buf)
             print('.', end='')
             if do_encrypt:
-                write_handle.write(buf.encode(encoding, errors='strict'))
+                write_handle.write(buf)
             else:
-                write_handle.write(buf.decode(encoding, errors='replace'))
+                # write_handle.write(buf.decode(encoding, errors='replace'))
+                write_handle.write(buf)
             if len(buf) < bufsize:
                 if do_encrypt:
                     print('successfully encrypted {} into {}'