title: Produce the ECB penguin with AES in python
body: <p>I want to encrypt data in an image but the resulting ciphertext to still be a valid image. I encrypt the image with AES in python and then, I replace header in the files, but windows can't open the encrypted image.</p>
  
  <p>Code</p>
  
  <pre><code>def encrypt_file(self, in_filename, out_filename):
      filesize = os.path.getsize(in_filename)
      iv = Random.new().read(AES.block_size)
      cipher = AES.new(self.key, AES.MODE_ECB, iv)
      chunksize = 64 * 1024
      with open(in_filename, 'rb') as infile:
          with open(out_filename, 'wb') as outfile:
              outfile.write(struct.pack('&lt;Q', filesize))
              outfile.write(iv)
  
              while True:
                  chunk = infile.read(chunksize)
                  if len(chunk) == 0:
                      break
                  elif len(chunk) % 16 != 0:
                      chunk += ' ' * (16 - len(chunk) % 16)
                  cifrado = base64.b64encode(cipher.encrypt(chunk))
                  print cifrado
                  outfile.write(cipher.encrypt(chunk))
  </code></pre>
  
  <p>I want this effect:
  <a href="https://filippo.io/the-ecb-penguin/" rel="nofollow">The ECB Penguin</a></p>