title: Encrypting files with AES on Android
body: <p>So I am working on a personal project for myself, and I am trying to encrypt files on my phone. These files can be anything i.e Documents, photos, etc. Right now I am trying to get this working properly. When ever I run the encryption it seems to work properly and encrypt the files. When I run the decrypt, sometimes it works and other times it doesn't. When it fails I generally get a "Error while finalizing cipher, pad block corrupted" error. I'm also not using different test files, so it isn't like some files work and others don't. It's the same two files I try each time. </p>
  
  <pre><code>public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
      FileInputStream fis = new FileInputStream(path);
      FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
      byte[] key = (salt + Pass).getBytes("UTF-8");
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      key = sha.digest(key);
      key = Arrays.copyOf(key,16);
      SecretKeySpec sks = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.ENCRYPT_MODE, sks);
      CipherOutputStream cos = new CipherOutputStream(fos, cipher);
      int b;
      byte[] d = new byte[8];
      while((b = fis.read(d)) != -1) {
          cos.write(d, 0, b);
      }
      cos.flush();
      cos.close();
      fis.close();
  }
  
  public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
      FileInputStream fis = new FileInputStream(path);
      FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
      byte[] key = (salt + Pass).getBytes("UTF-8");
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      key = sha.digest(key);
      key = Arrays.copyOf(key,16);
      SecretKeySpec sks = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, sks);
      CipherInputStream cis = new CipherInputStream(fis, cipher);
      int b;
      byte[] d = new byte[8];
      while((b = cis.read(d)) != -1) {
          fos.write(d, 0, b);
      }
      fos.flush();
      fos.close();
      cis.close();
  }
  </code></pre>
  
  <p>Currently the Salt and Password are static and do not change for testing purposes. Still get errors about half the time.</p>
  
  <p>Does anyone have any ideas on why this happens? I have been searching around and I have found a couple things to try, none of which worked. I have looked through some of the following questions for solutions:</p>
  
  <p><a href="https://stackoverflow.com/questions/26426151/android-decryption-error-while-finalizing-cipher">Android decryption: Error while finalizing cipher</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12799726/last-block-incomplete-with-cipherinputstream-cipheroutputstream-even-with-paddi">last block incomplete with CipherInputStream/CipherOutputStream, even with padding AES/CBC/PKCS5Padding</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13383006/encryption-error-on-android-4-2">Encryption error on Android 4.2</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11503157/decrypting-error-no-iv-set-when-one-expected">Decrypting error : &quot;no iv set when one expected&quot;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4598873/how-to-handle-last-block-incomplete-in-decryption">How to handle &quot;last block incomplete in decryption&quot;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12136558/encryption-and-decryption-of-image-file">Encryption and decryption of image file</a></p>
  
  <p><a href="https://stackoverflow.com/questions/13527521/tips-on-encryption-decryption-of-images-in-java-using-aes">Tips on encryption/decryption of images in java using AES</a></p>
  
  <p>Any help is greatly appreciated! I think I am just missing something simple...</p>
  
  <p>Update! </p>
  
  <p>People were right when it was the salt. When I removed the salt, the problem was solved... Did a little more digging, turns out salt+Pass was the issues, but because the salt was a byte[] and Pass was a string. I changed salt to String and then used salt.concat(Pass) and the problem was solved!</p>