android视频加密解密

来源:互联网 发布:caffe.io.transformer 编辑:程序博客网 时间:2024/05/21 06:49

4M的视频加密了一分钟。时间是硬伤。

package com.example.videoencrypt;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.CipherInputStream;import javax.crypto.CipherOutputStream;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.SecretKeySpec;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.MediaController;import android.widget.Toast;import android.widget.VideoView;/** * @author henry_wang * date:2015-11-24 * time: 下午10:24:45 */public class MainActivity extends Activity {private static final String filePath="/sdcard/20151123_233943.mp4";private static final String outPath="/sdcard/encrypt_henry.mp4";private static final String inPath="/sdcard/decrpt_henry.mp4";MediaController mc;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button encryptButton=(Button) findViewById(R.id.main_Encrypt);Button DecryptButton=(Button) findViewById(R.id.main_Decrypt);VideoView video=(VideoView) findViewById(R.id.video);mc=new MediaController(this);String uri="http://english-video.oss-cn-hangzhou.aliyuncs.com/whlei.mp4";video.setVideoURI(Uri.parse(inPath));video.setMediaController(mc);mc.setMediaPlayer(video);video.requestFocus();video.start();encryptButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {try {encrypt();Toast.makeText(getApplicationContext(), "henry_wang==加密完成", Toast.LENGTH_SHORT).show();} catch (InvalidKeyException e) {e.printStackTrace();}catch (NoSuchAlgorithmException e) {e.printStackTrace();}catch (NoSuchPaddingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}});DecryptButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {try {decrypt();Toast.makeText(getApplicationContext(), "henry_wang==解密完成", Toast.LENGTH_SHORT).show();} catch (InvalidKeyException e) {e.printStackTrace();}catch (NoSuchAlgorithmException e) {e.printStackTrace();}catch (NoSuchPaddingException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}}});}protected static void encrypt() throws NoSuchAlgorithmException,NoSuchPaddingException, InvalidKeyException, IOException {FileInputStream fis=new  FileInputStream(filePath);FileOutputStream fos=new FileOutputStream(outPath);SecretKeySpec sks=new SecretKeySpec("MyDifficultPassw".getBytes(),"AES");Cipher cipher=Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, sks);CipherOutputStream cos=new CipherOutputStream(fos, cipher);int b;byte[] d=new byte[8];while ((b=fis.read(d))!=-1) {cos.write(d,0, b);}cos.flush();cos.close();fis.close();}protected void decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IOException {FileInputStream fis=new FileInputStream(outPath);FileOutputStream fos=new FileOutputStream(inPath);SecretKeySpec sks=new SecretKeySpec("MyDifficultPassw".getBytes(),"AES");Cipher cipher=Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, sks);CipherInputStream cis=new CipherInputStream(fis, cipher);int b;byte[] d=new byte[8];while ((b=cis.read(d))!=-1) {fos.write(d,0,b);}fos.flush();fos.close();cis.close();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

0 0
原创粉丝点击