2012-03-26 70 views
1

我有一堆视频文件需要放在Android平板电脑上作为我们应用程序的数据。因为我们不想轻松访问,所以我们决定加密视频。我通过默默无闻的方式来采取安全措施。我之前开始通过加密整个视频并在播放之前对其进行解密,但很快显而易见,加载视频是一个很大的保真杀手。用户体验和应用程序的流程被拍摄。部分文件加密

我想也许我可以加密视频的第一个MB,因为我们所有的视频都超过1MB。如果盗贼至少拿到了他们的视频,那么这不是整个事情,并且是无用的。

贝娄是加密和解密文件的代码。它的工作原理除了视频,因为我试图将文件的两个部分拼凑在一起。加密文件看起来很好。解密的文件在一个地方关闭。我想通过差异测试来运行它们。

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)) >= 0) { 

      if(count <= 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)) >= 0 && count <= 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)) >= 0){ 

      out.write(buf,0,numRead); 

     } 

     out.close(); 
    } catch (java.io.IOException e) { 
    } 
} 

我想知道是否有人可以发现加密或解密的问题。我知道这不是一个理想的解决方案,但它适用于我们的情况。

谢谢。

+0

我不确定我是否理解你的问题。您提供的代码适用于某些文件,但不适用于视频文件?使用哪种视频(mpeg?)它不起作用?它工作,如果你使用整个文件的程序,而不是1兆? – woliveirajr 2012-03-26 20:35:39

+0

你问的是错误的问题。问题应该是:如何阻止Android用户复制我的视频?这就是[DRM api](http://developer.android.com/reference/android/drm/package-summary.html)的用途。 – mikerobi 2012-03-26 20:47:29

回答

2

您不会停止阅读或准确写入1048576字节,您可以读取/写入缓冲区跨过该阈值的任何部分。在读/写情况下,数量的大小不能保证是相同的,因此不一致。

解决方案是准确读入1048576字节的明文,通过加密程序写出来,然后继续执行文件的其余部分。同样在解密的情况下。

当然你也必须确保size(cleartext) == size(ciphertext)

1

那么,当你达到这一点:

if(count <= 1048576){ 
    count += numRead; 
    out_c.write(buf, 0, numRead); 
} 

让我们说,如果说之前的计数是1,048,575(只有1个字节丢失达到最大)。你的缓冲区是1024字节。所以,当你进入if语句时,你的计数结束为1,049,599。

因此,您的计数可能大于1,048,576。在阅读文件之前你需要这个实际的数字。

+0

我知道你们在某些方面是正确的,但进一步的调查显示第一个MB被正确解密。接下来的几个1024字节也很好。在某些时候它丢失了大约1024字节的数据。当我在解密方法中写出未解密的流时,循环运行n次,然后写出任何数据。我必须找出在哪里跳到。嗯。 – user1086377 2012-03-27 14:08:21