java简单的文件加密

文件加密是建立在文件复制的基础之上,说白了就是将一个文件复制过去,期间加上一些干扰信息,致使文件发生改变,来达到文件加密的效果.

文件解密是根据文件相应的加密,来进行解密.

本次加密是比较简层次的加密,用到了java中字节流,io字节流

io字节流,写的时候需要一个数组,假如这个字节数组大小是100.

复制文件,先读文件,再写文件,我读的时候调用read(byte[] b)方法,可以使用read(byte[] b,st,end);这里假设读进去是20-30.

st代表从数组的第st个写end个字节.

数组剩下的部分我用-128-127的随机数填进去

写的时候把整个数组写进去,到最后如果读出来不够100个,就直接写read(byte[] b,0,len),len代表读的有效字节数.

文件解密:

解密是每次读100个,读的时被加密的文件.

但是写的时候不能全写,因为其中只有20-30是有效的字节,以及最后一行的所有事有效的

因此解密就是写的时候只写20-30.到最后一行,不满100的就全写,因为我加密的时候最后一行如果不满100,我是全读出来的.

附上代码:

package com.info.test.jiami;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class MyEncryption {
	private static int start= 20;
	private static int size= 100;
	private static int stlen= 30;
	public static void main(String[] args) {
		/* 源文件 */
		String path = "F:\\test2\\1.png";
		/* 加密文件 */
		String path2 = "F:\\test2\\2.png";
		/* 解密文件 */
		String path3 = "F:\\test2\\3.png";
		File file = new File(path);
		File file2 = new File(path2);
		File file3 = new File(path3);
		/* 文件加密 */
//		Encry(file, file2);
		/* 文件解密 */
		Decrypt(file2,file3);
	}
	/**
	 * 文件解密
	 * @param f1
	 * @param f2
	 */
	private static void Decrypt(File f1, File f2) {
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		int count = 0;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
                /* 准备了一个字节数组,将文件读到数组中,读一次,写一次,直到读完 */
		byte[] b = new byte[size];
		int len = 0;
		try {
			fis = new FileInputStream(f1);
			bis = new BufferedInputStream(fis);
			
			fos = new FileOutputStream(f2);
			bos = new BufferedOutputStream(fos);
			while((len = bis.read(b))!=-1){
				System.out.println(count + "--->" + len + "--->" + Arrays.toString(b));
				if(len==size) {
                                        /* 长度是满的时候就写中间的 */
					bos.write(b,start,stlen);
				}else {
                                        /* 最后一行全写 */
					bos.write(b,0,len);
				}
				count++;
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if (bis != null) {
					bis.close();
					bis = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null) {
					bos.close();
					bos = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	/**
	 * 文件加密
	 * @param f1
	 * @param f2
	 */
	private static void Encry(File f1, File f2) {
		// TODO Auto-generated method stub
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		int count = 0;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		byte[] b = new byte[size];
		int len = 0;
		try {
			fis = new FileInputStream(f1);
			bis = new BufferedInputStream(fis);
			
			fos = new FileOutputStream(f2);
			bos = new BufferedOutputStream(fos);
			while ((len = bis.read(b, start, stlen)) != -1) {
				System.out.println(count + "--->" + len + "--->" + Arrays.toString(b));
				for (int i = 0; i < start; i++) {
					Random random = new Random();
					b[i]= (byte) (random.nextInt(256)-128);
				}
				for (int i = len+start+1; i < size; i++) {
					Random random = new Random();
                                        /* 将产生的随机数加到数组中,构成干扰信息 */
					b[i]= (byte) (random.nextInt(256)-128);
				}
				if (len == stlen) {
					bos.write(b, 0, size);
				} else {
					bos.write(b, start, len);
				}
				count++;
			}
			bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null) {
					bis.close();
					bis = null;
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null) {
					bos.close();
					bos = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

前边的几个参数是我设置的全局变量,有利于修改加密的起始位置.我设置的时字节数组大小100,从20开始,往后面写30个.

用到的主要是java中的io字节流,不懂复制的可以看下我的另一篇文章,里面讲的是如何复制文件或者文件夹.

猜你喜欢

转载自blog.csdn.net/crg18438610577/article/details/82181218