2012-07-20 29 views
1

我必須閱讀Android設備上的加密視頻。Android上的AES密碼無法讀取crypt視頻

我創建使用http://elonen.iki.fi/code/nanohttpd/本地主機服務器允許這在一個非常簡單的方法。

我用它實現閱讀未加密的影片,但我卡在讀取加密的視頻。 (我沒有任何麻煩加密視頻,只是爲了閱讀)

我試過如下:

1-要使用一個簡單的「AES」加密視頻,當我嘗試閱讀它與我的服務器,我看到流開始(我看到我的服務器回答3次各種範圍)。 經過3次玩家表示無法閱讀視頻。

2到加密使用「AES/CTR/NoPadding」的視頻:在這種情況下,我看到我的服務器一而再,再而再次提供第一範圍和ISS運行,但沒有視頻顯示。

我嘗試用CTR16得到的16位塊,並與32KO的BUFER閱讀。這是行不通的。

(PS:我有uncrypt畫面沒有問題,我的方法)

這裏我隱窩方法:

public static InputStream getUncryptInputStream(InputStream is, String pass, final long dataLen) throws Exception{ 
     SecretKeySpec key = new SecretKeySpec(getRawKey(pass.getBytes()), "AES"); 
     Cipher mCipher = Cipher.getInstance("AES/CTR/NoPadding"); 
     mCipher.init(Cipher.DECRYPT_MODE, key, ivSpec); 
     if(dataLen==-1){ 
      return new CipherInputStream(is, mCipher); 
     }else{ 
      return new CipherInputStreamWithDataLen(is, mCipher, dataLen); 
     } 
} 

public static OutputStream getCryptOutputStream(OutputStream os, String pass) throws Exception{ 
    SecretKeySpec key = new SecretKeySpec(getRawKey(pass.getBytes()), "AES"); 

    Cipher mCipher = Cipher.getInstance("AES/CTR/NoPadding"); 
    mCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); 

    return new CipherOutputStream(os, mCipher); 

} 

private static byte[] getRawKey(byte[] seed) throws Exception { 
    KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); 
    sr.setSeed(seed); 
    kgen.init(128, sr); // 192 and 256 bits may not be available 
    SecretKey skey = kgen.generateKey(); 
    byte[] raw = skey.getEncoded(); 
    return raw; 
} 

和我創建,因爲可用的方法總是返回0與正常CipherInputStreamWithDataCipherInputStreamWithData

import java.io.IOException; 
import java.io.InputStream; 

import javax.crypto.Cipher; 
import javax.crypto.CipherInputStream; 

public class CipherInputStreamWithDataLen extends CipherInputStream{ 

    int dataLen; 

    public CipherInputStreamWithDataLen(InputStream is, Cipher mCipher, long dataLen) { 
     super(is, mCipher); 
     this.dataLen = (int)dataLen; 
     // TODO Auto-generated constructor stub 
    } 

    public int available() throws IOException{ 
     return dataLen; 
    } 

} 
+0

問題來自CipherInputStream。 – Yop 2012-07-24 12:25:16

回答

1

問題出在CipherInputStream skip方法。

  1. 因爲暗號encryptation的,你要小心跳過,直到最後的密文分組,然後解密最後+ 1 cupherblock得到你需要編寫到您的BufferedOutputStream
  2. 如果值的一些額外的字節CipherInputStream中的「available」方法的返回值爲<到要跳過的字節數,skip方法將跳過的最大字節數將等於「available」方法返回的值。

一般說明:在視頻開始前,需要大量的BufferedInputStream,需要10秒。 有些人建議使用NDK在本地編碼AES。 爲了我的需要,我只編寫了一個簡單的XOR。