图片,音频,视频的文件加密,解密,保护自身资源

 

代码比较简单,不做解释

主界面调用方法,具体使用场景,需要根据自己的需求去封装

package com.demo;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import thread.FileCodeListener;
import thread.FileCodeRunnable;
import thread.FileUnCodeRunnable;
import thread.FileUtil;

public class TestDemo {

	static ExecutorService executor = Executors.newFixedThreadPool(10);

	public static void main(String[] args) {
		// codeFile();
		unCodeFile();
	}

	/**
	 * 文件解密方法
	 */
	private static void unCodeFile() {
		String Path = "G:\\filmVideo\\417\\american";
		List<File> lists = FileUtil.getFileListFromPath(Path);
		if (lists == null || lists.size() < 1) {
			System.out.println("====文件夹的个数 0 ==");
			return;
		}
		System.out.println("====文件夹的个数==" + lists.size());
		for (int i = 0; i < lists.size(); i++) {
			String fileName = lists.get(i).getName();
			String filePath = lists.get(i).getAbsolutePath();
			String cachePath = "G:\\filmVideo\\417\\americanCache\\" + fileName;
			FileUnCodeRunnable runnbale = new FileUnCodeRunnable(filePath, cachePath, new FileCodeListener() {

				@Override
				public void codeFileStatues(boolean isSuccess, String desc) {
					System.out.println("====codeFileStatues====" + isSuccess + " / " + desc);
				}

				@Override
				public void codeFileProgress(int progress) {
					System.out.println("====progress====" + progress);

				}
			});
			executor.execute(runnbale);
		}
	}

	/**
	 * 文件加密
	 */
	private static void codeFile() {
		String filePath = "E:\\log\\aaa.mp4";
		String cachePath = "E:\\log\\bbb.mp4";

		FileCodeRunnable runnbale = new FileCodeRunnable(cachePath, filePath, new FileCodeListener() {

			@Override
			public void codeFileStatues(boolean isSuccess, String desc) {
				System.out.println("====codeFileStatues====" + isSuccess + " / " + desc);
			}

			@Override
			public void codeFileProgress(int progress) {
				System.out.println("====progress====" + progress);

			}
		});
		executor.execute(runnbale);
	}
}

 

package thread;

public class Config {

	public static final String PASSWORD = "123456";

}

 转码回调类

package thread;

public interface FileCodeListener {

	// 转码进度
	void codeFileProgress(int progress);

	// 转码状态
	void codeFileStatues(boolean isSuccess, String desc);

}
package thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;

/**
 * 文件加密方法
 * 
 * @author Administrator
 *
 */
public class FileCodeRunnable implements Runnable {

	String filePath;
	String cachePath;
	FileCodeListener listener;
	long fileLenth = 1;

	public FileCodeRunnable(String filePath, String cachePath, FileCodeListener listener) {
		this.filePath = filePath;
		this.cachePath = cachePath;
		this.listener = listener;
	}

	@Override
	public void run() {
		File file = new File(filePath);
		if (!file.exists()) {
			listener.codeFileStatues(false, "文件不存在");
			return;
		}
		fileLenth = file.length();
		enCode();
	}

	public void enCode() {
		InputStream in = null;
		OutputStream out = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				return;
			}
			in = new FileInputStream(filePath);
			out = new FileOutputStream(cachePath);
			byte[] buffer = new byte[1024];
			byte[] buffer2 = new byte[1024];
			int readLength;
			long sumLength = 0;
			while ((readLength = in.read(buffer)) > 0) {
				for (int i = 0; i < readLength; i++) {
					byte b = buffer[i];
					buffer2[i] = b == 255 ? 0 : ++b;// 文件的每一byte增1
				}
				sumLength = sumLength + readLength;
				int progress = (int) (sumLength * 100 / fileLenth);
				listener.codeFileProgress(progress);
				out.write(buffer2, 0, readLength);
				out.flush();
			}
			appendTail(cachePath, Config.PASSWORD);
			listener.codeFileStatues(true, "转码完成");
		} catch (Exception e) {
			listener.codeFileStatues(true, "转码异常:" + e.toString());
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	/**
	 * 在文件尾添加指定的字符串  
	 * 
	 * @param fileName
	 * @param content
	 */
	private static void appendTail(String fileName, String content) {
		try {
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.writeBytes(content);
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

解密工具类

package thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件解密方法
 * 
 * @author Administrator
 *
 */
public class FileUnCodeRunnable implements Runnable {

	String filePath;
	String cachePath;
	FileCodeListener listener;
	long fileLenth = 1;

	public FileUnCodeRunnable(String filePath, String cachePath, FileCodeListener listener) {
		this.filePath = filePath;
		this.cachePath = cachePath;
		this.listener = listener;
	}

	@Override
	public void run() {
		File file = new File(filePath);
		if (!file.exists()) {
			listener.codeFileStatues(false, "文件不存在");
			return;
		}
		fileLenth = file.length();
		unCode();
	}

	public void unCode() {
		InputStream is = null;
		OutputStream out = null;
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				return;
			}
			File dest = new File(cachePath);
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
			is = new FileInputStream(filePath);
			out = new FileOutputStream(cachePath);

			byte[] buffer = new byte[1024];
			byte[] buffer2 = new byte[1024];
			byte bMax = (byte) 255;
			long size = file.length() - Config.PASSWORD.length();
			int mod = (int) (size % 1024);
			int div = (int) (size >> 10);
			int count = mod == 0 ? div : (div + 1);
			int k = 1;
			int readLength = 0;
			long sumWrite = 0; // 已经读写的
			while ((k <= count && (readLength = is.read(buffer)) > 0)) {
				if (mod != 0 && k == count) {
					readLength = mod;
				}
				for (int i = 0; i < readLength; i++) {
					byte b = buffer[i];
					buffer2[i] = b == 0 ? bMax : --b;// 文件的每一个byte减1
				}
				sumWrite = sumWrite + readLength;

				int progress = (int) (sumWrite * 100 / fileLenth);
				listener.codeFileProgress(progress);
				out.write(buffer2, 0, readLength);
				k++;
			}
			listener.codeFileStatues(true, "解码成功");
		} catch (Exception e) {
			listener.codeFileStatues(false, "解码异常:" + e.toString());
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (is != null) {
					is.close();
				}
			} catch (Exception e2) {

			}
		}
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值