2012-03-26 72 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