搭建直播網站android視頻加密解密

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;
	}
 
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章