title: Need help to encrypt/decrypt images
body: <p>I am using RSA to encrypt an image and Pillow to read it byte by byte. I am encrypting each 128-bytes. But when I try to decrypt it back, the resulting image it is not like the original. 
  Here is my code:</p>
  
  <pre><code>from Crypto.PublicKey import RSA
  from PIL import Image
  
  
  def genearteRSAKeys(keyLength):
      private = RSA.generate(keyLength)
      public = private.publickey()
      privateKey = private.exportKey()
      publicKey = public.exportKey()
      return privateKey, publicKey
  
  
  def rsaEncrypt(pubKey, data):
      publicKey = RSA.importKey(pubKey)
      encryptData = publicKey.encrypt(data, "")
      return encryptData
  
  def rsaDecrypt(pivKey, data):
      privateKey = RSA.importKey(pivKey)
      decryptData = privateKey.decrypt(data)
      return decryptData
  
  
  im = Image.open("photo.jpg")
  w, h = im.size
  data = im.tobytes()
  
  privateKey, publicKey = genearteRSAKeys(1024)
  
  step = 128
  block_cipher = []
  for i in range(0, len(data), step):
      encrypted = rsaEncrypt(publicKey, data[i:i+step])
      block_cipher.append(''.join(encrypted))
  
  data_cipher = ''.join(block_cipher)
  
  img = Image.frombytes("RGB", (w, h), data_cipher)
  img.save("photo2.jpg")
  
  image = Image.open("photo2.jpg")
  data_encrypt = image.tobytes()
  block_plant =[]
  for j in range(0, len(data_encrypt), step):
      decrypted = rsaDecrypt(privateKey, data_encrypt[j:j+step])
      block_plant.append(''.join(decrypted))
  
  data_plant = ''.join(block_plant)
  
  image2 = Image.frombytes("RGB", (w,h), data_plant)
  image2.show()
  </code></pre>
  
  <p>why isn't this code working?</p>