android视频加密解密

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;

	@Override
	protected 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() {
			@Override
			public 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() {
			@Override
			public 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();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值