title: Partial File Encryption
body: <p>I have a bunch of video files that need to be put onto Android tablets as data for our application. Because we do not want easy access we have decided to encrypt the videos. I'm taking the security through obscurity approach.  I started out by encrypting the whole video and decrypting it before playing but quickly it became apparent that loading videos is a big fidelity killer. The user experience and the flow of the application is shot.</p>
  
  <p>I thought maybe I can encrypt the first MB of the video considering all of our videos are over 1MB. If the thief gets their hand on the video at least it isn't the whole thing and is kind of useless. </p>
  
  <p>Bellow is the code that encrypts and decrypts files. It works except for the videos because I'm trying to patch two parts of a file together.  The encrypted file seems fine.  The decrypted file is off in one spot. I figured that but running them through a diff test.</p>
  
  <pre><code> public void encrypt(InputStream in, OutputStream out) {
      try {
          // Bytes written to out will be encrypted
         OutputStream out_c = new CipherOutputStream(out, ecipher);
  
          // Read in the cleartext bytes and write to out to encrypt
          int numRead = 0;
          int count = 0;
          boolean first = true;
  
          while ((numRead = in.read(buf)) &gt;= 0) {
  
              if(count &lt;= 1048576){
                  count += numRead;
                  out_c.write(buf, 0, numRead);
              }else{
  
                  out.write(buf, 0, numRead);
  
              }
          }
          out.close();
          out_c.close();
  
  
      } catch (java.io.IOException e) {
      }
  }
  
  // Movies encrypt only 1 MB.
  
  public void decrypt(InputStream in, OutputStream out) {
      try {
          // Bytes read from in will be decrypted
          InputStream in_c = new CipherInputStream(in, dcipher);
  
          // Read in the decrypted bytes and write the cleartext to out
          int numRead = 0;
          int count = 0;
  
          while ((numRead = in_c.read(buf)) &gt;= 0 &amp;&amp; count &lt;= 1048576) {
              count += numRead;
              out.write(buf, 0, numRead);
          }
  
          //in.skip(count); This removed 1MB from the final file. No need to skip.
  
          while((numRead = in.read(buf)) &gt;= 0){
  
              out.write(buf,0,numRead);
  
          }
  
          out.close();
      } catch (java.io.IOException e) {
      }
  }
  </code></pre>
  
  <p>I was wondering if anyone can spot the problem with the encryption or decryption.  I know it isn't an ideal solution but it works in our situation.</p>
  
  <p>Thank you.</p>