title: How do i decrypt a file in Android with AES?
body: <p>I have a file (mp3) that i Encrypt, my intention is then to download this file to an android device and decrypt it, but during decryption i get an IOException:</p>
  
  <p><code>java.io.IOException: last block incomplete in decryption</code></p>
  
  <p>I am aware of the obvious security flaws in the following code, I'm just trying to get it to work first.</p>
  
  <p>Any help on this is appreciated and I am quite a newcomer to coding in general so excuse me in advance if this is a stupid question!</p>
  
  <p>Encryption class(Not done in Android, works):</p>
  
  <pre><code>public class EncryptFile {
  public static void main(String args[]) {
      if (args.length &lt; 1) {
          System.out.println("Usage: java EncryptFile &lt;file name&gt;");
          System.exit(-1);
      }
      try {
          File aesFile = new File("encodedfile.enc");
          FileInputStream fis;
          FileOutputStream fos;
          CipherInputStream cis;
           //Creation of Secret key
          String key = "mysecretkey";
          int length=key.length();
          if(length&gt;16 &amp;&amp; length!=16){
              key=key.substring(0, 15);
          }
          if(length&lt;16 &amp;&amp; length!=16){
              for(int i=0;i&lt;16-length;i++){
                  key=key+"0";
              }
          }
          SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
           //Creation of Cipher objects
          Cipher encrypt =Cipher.getInstance("AES/CBC/PKCS5Padding");
  
          byte[] aByte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  
  
              IvParameterSpec ivSpec = new IvParameterSpec(aByte);
  
          encrypt.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
          try {
              fis = new FileInputStream(args[0]);
              cis = new CipherInputStream(fis,encrypt);
              // Write to the Encrypted file
              fos = new FileOutputStream(aesFile);
              byte[] b = new byte[8];
              int i = cis.read(b);
              while (i != -1) {
                  fos.write(b, 0, i);
                  i = cis.read(b);
              }
              fos.flush();
              fos.close();
              cis.close();
              fis.close();
          } catch(IOException err) {
              System.out.println("Cannot open file!");
              System.exit(-1);
          }
      } catch(Exception e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Decryption (In Android device):</p>
  
  <pre><code>    public class DecryptFile {  
    public static File main(String args[], File encFile, Context context) { 
        for (int i = 0; i &lt; args.length; i++) {
          Log.i("ARGS", args[i]);
      }
         try {  
              File aesFile = new File(args[0]);  
  
              aesFile= encFile;  
              Log.d("AESFILELENGTH", "aes length: " + aesFile.length());
              File aesFileBis = new File(context.getFilesDir(), args[0]);
  
              FileInputStream fis;  
              FileOutputStream fos;  
              CipherInputStream cis;  
  
   //Creation of Secret key  
              String key = "mysecretkey";  
              int length=key.length();  
              if(length&gt;16 &amp;&amp; length!=16){  
                   key=key.substring(0, 15);  
              }  
              if(length&lt;16 &amp;&amp; length!=16){  
                   for(int i=0;i&lt;16-length;i++){  
                        key=key+"0";  
                   }  
              }  
              SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");  
               //Creation of Cipher objects  
              Cipher decrypt =Cipher.getInstance("AES/CBC/PKCS5Padding");  
  
              byte[] aByte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  
  
              IvParameterSpec ivSpec = new IvParameterSpec(aByte);
  
              decrypt.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);  
  
   // Open the Encrypted file  
  
              fis = new FileInputStream(aesFile);  
  
              cis = new CipherInputStream(fis, decrypt);  
  
              // Write to the Decrypted file  
              fos = new FileOutputStream(aesFileBis);  
              try {
  
                  byte[] mByte = new byte[8];  
                  int i = cis.read(mByte); 
                  Log.i("MBYTE", "mbyte i: " + i);
                  while (i != -1) {  
                      fos.write(mByte, 0, i);  
                      i = cis.read(mByte);  
                  }  
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
  
              fos.flush();  
              fos.close();  
              cis.close();  
              fis.close(); 
              return aesFileBis;
         } catch(Exception e){  
              e.printStackTrace();  
         }
      return null;  
    }  
  }
  </code></pre>