29,028
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Test {
public static final String PATH="E:\\workspaces\\testwx\\files";
/**
* 加密
* @throws Exception
*/
public void encode()throws Exception {
//获取密钥文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(PATH+"\\password")));
byte[] passwd=new byte[bis.available()];
bis.read(passwd);
bis.close();
//获取原视频文件
BufferedInputStream bisOld = new BufferedInputStream(new FileInputStream(new File(PATH+"\\asd.mp4")));
//输出加密后的流文件
BufferedOutputStream bosNew = new BufferedOutputStream(new FileOutputStream(new File(PATH+"\\encode\\asd_new")));
//写入密钥
bosNew.write(passwd);
byte[] buffer=new byte[1024];
int len=0;
//加密文件
while((len=bisOld.read(buffer))>0){
bosNew.write(buffer, 0, len);
}
bosNew.flush();
bosNew.close();
bisOld.close();
}
/**
* 解密
* @throws Exception
*/
public void decode()throws Exception {
//获取密钥文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(PATH+"\\password")));
int passwdLen=bis.available();
byte[] passwd=new byte[passwdLen];
bis.close();
//获取加密文件
BufferedInputStream bisOld = new BufferedInputStream(new FileInputStream(new File(PATH+"\\encode\\asd_new")));
//输出解密后流文件
BufferedOutputStream bosNew = new BufferedOutputStream(new FileOutputStream(new File(PATH+"\\decode\\asd.mp4")));
//获取密钥
bisOld.read(passwd);
passwd=null;
byte[] buffer=new byte[1024];
int len=0;
while((len=bisOld.read(buffer))>0){
bosNew.write(buffer, 0, len);
}
bosNew.flush();
bosNew.close();
bisOld.close();
}
public static void main(String[] args)throws Exception {
Test t = new Test();
//加密
t.encode();
//解密
t.decode();
}
}