动态解密mp4

时间:2011-10-16 18:40:06

标签: java android encryption

让我解释一下我想做什么。 我有一个加密的mp4文件,使用AES / CBC / PKCS5Padding模型。现在我把它放到我自己的网络服务器上为我自己的Android网络MediaPlayer通过网络流mp4播放它。

我使用CipherInputstream来解密加密的mp4文件并放入InputStreamEntity,但这不起作用,所以我需要任何建议如何做。

有些代码, ...

public void InitCiphers()
            throws NoSuchAlgorithmException,
            NoSuchProviderException,
            NoSuchProviderException,
            NoSuchPaddingException,
            InvalidKeyException,
            InvalidAlgorithmParameterException{

   //1. create the cipher using Bouncy Castle Provider
   encryptCipher =
           Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
   //2. create the key
   SecretKey keyValue = new SecretKeySpec(key,"AES");
   //3. create the IV
   AlgorithmParameterSpec IVspec = new IvParameterSpec(IV);
   //4. init the cipher
   //encryptCipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec);
   encryptCipher.init(Cipher.ENCRYPT_MODE, keyValue);

   //1 create the cipher
   decryptCipher =
           Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
   //2. the key is already created
   //3. the IV is already created
   //4. init the cipher
   //decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec);
   decryptCipher.init(Cipher.DECRYPT_MODE, keyValue);
}

public void CBCEncryptCipherStream(InputStream fis, OutputStream fos) throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException{
    byte[] buf = new byte[1024];

    fos = new CipherOutputStream(fos, encryptCipher);

    int numRead = 0;
    while ((numRead = fis.read(buf)) >= 0) {
        fos.write(buf, 0, numRead);
    }
    fos.close();
}

public InputStream CBCDecryptStream(InputStream encryptedInputStream){
    InputStream decryptedInputStream = null;
    decryptedInputStream = new CipherInputStream(encryptedInputStream, decryptCipher);
    return decryptedInputStream;

}

然后,

InputStream datastream = bcAES_CBC_128.CBCDecryptStream(new FileInputStream(encyptedPlayFile));

bodystream= new InputStreamEntity(datastream, filesize);

response.setEntity(bodystream);
bodystream.setContentType("video/mpeg");

0 个答案:

没有答案