title: How to download an encrypted png file without loosing data?
body: <p>I'm new to android. I'm doing an application which can download an encrypted png image file to sd card then displaying it after decryption. But I notice that I'm getting "<code>javax.crypto.IllegalBlockSizeException: last block incomplete in decryption image decryption</code>" when decrypting that downloaded image. Then I found that downloaded image size is 0KB (Original - 150KB). Then I downloaded my encrypted image from browser and checked. I'm getting the original image size. I'm sure there is something wrong in my image downloading class. But I cant figure it out. Please help me. Thanks in advance.</p>
  
  <p>Image downloading AsyncTask Class</p>
  
  <pre><code>public class DownloadImagesTask extends AsyncTask&lt;String, Void, Bitmap&gt;
  {
      private String fileName;
  
      @Override
      protected Bitmap doInBackground(String... urls)
      {
          //Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
          return download_Image(urls[0]);
      }
  
      @Override
      protected void onPostExecute(Bitmap result)
      {
          storeImage(result);
  
      }
  
      private Bitmap download_Image(String url)
      {
          Bitmap bm = null;
          File file = new File(url);
          fileName = file.getName();
          try
          {
              URL aURL = new URL(url);
              URLConnection conn = aURL.openConnection();
              conn.connect();
              InputStream is = conn.getInputStream();
              BufferedInputStream bis = new BufferedInputStream(is);
              bm = BitmapFactory.decodeStream(bis);
              bis.close();
              is.close();
          }
          catch (OutOfMemoryError e)
          {
              Log.e("Hub", "Error getting the image from server : " + e.getMessage().toString());
          }
          catch (IOException e)
          {
              Log.e("Hub", "Error getting the image from server : " + e.getMessage().toString());
          }
  
          return bm;
      }
  
      public void storeImage(Bitmap bm)
      {
          BitmapFactory.Options bmOptions;
          bmOptions = new BitmapFactory.Options();
          bmOptions.inSampleSize = 1;
          String extStorageDirectory = CommonUtils.getDataFromPreferences("metaPath", "");
  
          Log.d("extStorageDirectory", extStorageDirectory);
          OutputStream outStream = null;
  
          File wallpaperDirectory = new File(extStorageDirectory);
          if (!wallpaperDirectory.exists())
          {
              wallpaperDirectory.mkdirs();
          }
          File outputFile = new File(wallpaperDirectory, fileName);
          if (!outputFile.exists() || outputFile.length() == 0)
          {
              try
              {
                  outStream = new FileOutputStream(outputFile);
              }
              catch (FileNotFoundException e1)
              {
                  e1.printStackTrace();
              }
  
              try
              {
                  bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                  outStream.flush();
                  outStream.close();
                  Log.d("ScratchActivtiy", "Image Saved");
  
              }
              catch (FileNotFoundException e)
              {
                  e.printStackTrace();
  
              }
              catch (IOException e)
              {
                  e.printStackTrace();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>(All images were encrypted by me. And I hosted them in a Server. There is no issue in ancryption or decryption. I tested them. All are working fine.)</p>
  
  <p>CryptClass</p>
  
  <pre><code>public class CryptClass
  {
  
      public byte[] encrypt(String seed, byte[] cleartext) throws Exception
      {
  
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] result = encrypt(rawKey, cleartext);
          //  return toHex(result);
          return result;
      }
  
      public byte[] decrypt(String seed, byte[] encrypted) throws Exception
      {
          byte[] rawKey = getRawKey(seed.getBytes());
          byte[] enc = encrypted;
          byte[] result = decrypt(rawKey, enc);
  
          return result;
      }
  
      //done
      private byte[] getRawKey(byte[] seed) throws Exception
      {
          KeyGenerator kgen = KeyGenerator.getInstance("AES");
          SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
          sr.setSeed(seed);
          kgen.init(128, sr);
          SecretKey skey = kgen.generateKey();
          byte[] raw = skey.getEncoded();
          return raw;
      }
  
      private byte[] encrypt(byte[] raw, byte[] clear) throws Exception
      {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          byte[] encrypted = cipher.doFinal(clear);
          return encrypted;
      }
  
      private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception
      {
          SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, skeySpec);
          byte[] decrypted = cipher.doFinal(encrypted);
          return decrypted;
      }
  }
  </code></pre>