在android中通过libmedia播放加密video

我正在播放来自SD卡的加密mp4video文件。 为此我做什么…..

步骤1.我从内存中的sdcard复制文件,我用世界可读和世界可写格式编写该文件,以便我可以播放

步骤2.我在temprary文件夹中解密该文件

STEP 3.现在我玩这个,文件没问题

问题是这个过程需要时间,因为文件大约25 mb,这个过程(复制和解密)大约需要2分钟的时间

经过一些阅读后我发现这个问题的解决方案我可以通过使用libmedia库来减少时间,但是我在使用libmedia库时出现这个问题我遇到了一些问题

10-10 06:33:42.908: E/MediaPlayer(9249): error (1, -2147483648) 

在活动屏幕上,我在对话框中收到一条消息“无法播放video”

libmedia库链接是http://libeasy.alwaysdata.net/

感谢您宝贵的建议和帮助

我的活动代码是

 package com.example.playvideo2; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import fr.maxcom.http.LocalSingleHttpServer; import fr.maxcom.libmedia.Licensing; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.view.Menu; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends Activity implements OnCompletionListener { LocalSingleHttpServer mServer; VideoView mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video_view); Licensing.allow(this); playENCVideo(Environment.getExternalStorageDirectory() + "/encVideo.mp4"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void playENCVideo(String path) { try { Cipher decipher = null; KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecretKey skey = kgen.generateKey(); decipher = Cipher.getInstance("AES"); decipher.init(Cipher.DECRYPT_MODE, skey); mServer = new LocalSingleHttpServer(); mServer.setCipher(decipher); mServer.start(); path = mServer.getURL(path); mVideoView = (VideoView) findViewById(R.id.videoView1); mVideoView.setVideoPath(path); // mVideoView.setMediaController(new MediaController(this)); // mVideoView.requestFocus(); mVideoView.start(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onCompletion(MediaPlayer mp) { // MediaPlayer.OnCompletionListener interface mServer.stop(); } } 

我的清单文件是

              

这是我用于加密和解密的代码

  FileInputStream fis = new FileInputStream(new File("D:/QLurnVideo/inputVideo.mp4")); File outfile = new File("D:/QLurnVideo/encVideo.mp4"); int read; if(!outfile.exists()) outfile.createNewFile(); File decfile = new File("D:/QLurnVideo/decVideo.mp4"); if(!decfile.exists()) decfile.createNewFile(); FileOutputStream fos = new FileOutputStream(outfile); FileInputStream encfis = new FileInputStream(outfile); FileOutputStream decfos = new FileOutputStream(decfile); Cipher encipher = Cipher.getInstance("AES"); Cipher decipher = Cipher.getInstance("AES"); KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecretKey skey = kgen.generateKey(); //Lgo encipher.init(Cipher.ENCRYPT_MODE, skey); CipherInputStream cis = new CipherInputStream(fis, encipher); decipher.init(Cipher.DECRYPT_MODE, skey); CipherOutputStream cos = new CipherOutputStream(decfos,decipher); while((read = cis.read())!=-1) { fos.write((char)read); fos.flush(); } fos.close(); while((read=encfis.read())!=-1) { cos.write(read); cos.flush(); } cos.close(); 

你是按字符加密video,这需要花费很多时间,通过使用这样的缓冲区逐行进行。

 byte[] buff=new byte[1024]; while((read = cis.read(buff))!=-1) { fos.write(buff,0,read); fos.flush(); } fos.close(); while((read=encfis.read(buff))!=-1) { cos.write(buff,0,read); cos.flush(); } cos.close(); 

您的问题主要是因为您不在加密代码和playENCVideo()中使用相同的SecretKey,因为generateKey()在每次调用时返回一个不同的随机项。

不要使用KeyGenerator,而是使用SecretKeySpec并保持两段代码之间的一致性。

其次,优选地使用流密码,例如ARC4,而不是分组密码。 AES的默认值是“AES / ECB”,它是块密码。

从版本1.0.1开始,Libmedia能够使用分组密码,但与上下文中的流密码相比,没有任何好处,只有烦恼。

例:

 Cipher c = Cipher.getInstance("ARC4"); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec("BrianIsInTheKitchen".getBytes(), "ARC4")); 

您使用不同的密钥进行加密和解密。 使用相同的密钥,它将工作。只需将加密密钥传递给playENCVideofunction并使用它。