title: Android Lollipop Decryption using AES is not working properly
body: <p>Till kitkat, encryption/decryption is working good but in lollipop it decrypts only partial data.</p>
  
  <p>I don't have problem with encryption because I encrypted a file with lollipop and decrypts it with kitkat it works fine but not vice versa.</p>
  
  <p>Here is the code.</p>
  
  <p><strong>Encryption code</strong></p>
  
  <pre><code>Encrypt(BufferedInputStream is, File destfile, String passcode) {
          bis = is;
          try {
              fos = new FileOutputStream(destfile);
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          dest = new BufferedOutputStream(fos, 1024);
          this.passcode = passcode;
      }
  
      static void encrypt() throws IOException, NoSuchAlgorithmException,
      NoSuchPaddingException, InvalidKeyException {
  
          // Length is 16 byte
          SecretKeySpec sks = new SecretKeySpec(passcode.getBytes(), "AES");
  
          // Create cipher
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.ENCRYPT_MODE, sks);
          // Wrap the output stream
          CipherOutputStream cos = new CipherOutputStream(fos, cipher);
          // Write bytes
          int b;
          byte[] d = new byte[1024];
          while ((b = bis.read(d)) != -1) {
              cos.write(d, 0, b);
          }
          // Flush and close streams.
          cos.flush();
          cos.close();
          bis.close();
      }
  </code></pre>
  
  <p><strong>Decryption code</strong></p>
  
  <pre><code>public Decrypt(String path, String pathcode) {
          // TODO Auto-generated constructor stub
          filepath = path;
          try {
              fis = new FileInputStream(new File(path));
              this.passcode = pathcode;
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      static String decrypt() throws IOException, NoSuchAlgorithmException,
      NoSuchPaddingException, InvalidKeyException {
  
          SecretKeySpec sks = new SecretKeySpec(passcode.getBytes(), "AES");
          Cipher cipher = Cipher.getInstance("AES");
          cipher.init(Cipher.DECRYPT_MODE, sks);
          CipherInputStream cis = new CipherInputStream(fis, cipher);
          int size = fis.available();
          byte[] resdata = new byte[size];
          cis.read(resdata, 0, size);
          String newres = new String(resdata, "UTF-8").trim();
          //write("decrypted_file.xhtml",newres);  
          if(fis!=null)
          {
          fis.close();
          }
          if(cis!=null)
              cis.close();
          return newres;
      }
  </code></pre>
  
  <p>What's the problem in this code? Do I need to do anything more?</p>