欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 自定义加密和解密文件

程序员文章站 2022-04-03 15:59:31
简单自定义 字节加1package game01;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.nio.channels.FileChannel;import java.util.Scanner;public class Tool {public s...

简单自定义 字节加1 有用记得点个赞

package game01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.Scanner;

public class Tool {
	public static FileInputStream fis =null;
	public static FileOutputStream fos =null;
	public static long start;//计时
	public static long end; 
	public static String jiamilj = null;
	public static String jiemilj = null;
	public static FileChannel fc = null;//文件大小
	
	
	public static void main(String []args) throws IOException{
		
		Scanner in = new Scanner(System.in);
		System.out.println("你要加密还是解密:");
		String input = in.next();
		
		if(input.equals("加密")){
			
			Scanner in01 = new Scanner(System.in);
			System.out.println("你要加密的文件路径:(E:\\a.txt):");
			String input01 = in01.next();
			System.out.println(input01);
			
			jiami(input01);
			System.out.println("加密成功,加密路径:"+jiamilj);
			System.out.println("加密用时"+(end-start)+"毫秒");
			size(1);
			System.out.println("文件大小"+fc.size()+"Byte");
			fis.close();
			fos.close();
		}else{
			Scanner in02 = new Scanner(System.in);
			System.out.println("你要解密的文件路径:(E:\\b.txt):");
			String input02 = in.next();
			jiemi(input02);
			System.out.println("解密成功,解密路径为:"+jiemilj);
			System.out.println("解密密用时"+(end-start)+"毫秒");
			size(2);
			System.out.println("文件大小"+fc.size()+"Byte");
			fis.close();
			fos.close();
		}	
		
		
	}
	
	
	//加密
	public static void jiami(String input02) throws IOException{
		start = System.currentTimeMillis();
		in01(input02);
		end = System.currentTimeMillis();
	}
	//解密
	public static void jiemi(String input02) throws IOException{
		start = System.currentTimeMillis();
		in02(input02);
		end = System.currentTimeMillis();
	}
	
	
	//配套加密
	public static void in01(String input03) throws IOException{
		 fis = new FileInputStream(input03);		
		int i;  //核心代码  
		while((i=fis.read())!=-1){			
			out01(i);
		}
		
	}	
	public static void out01(int i) throws IOException{
		 fos = new FileOutputStream("E:\\b.txt",true);
		fos.write(i+1);	
		jiamilj = "E:\\b.txt";
	}
	
	
	//配套解密
	public static void in02(String input03) throws IOException{
		fis = new FileInputStream(input03);		
		int i;  //核心代码  
		while((i=fis.read())!=-1){
			out02(i);
		}	
	}
	public static void out02(int i) throws IOException{
		 fos = new FileOutputStream("E:\\c.txt",true);
		fos.write(i-1);	
		jiemilj = "E:\\c.txt";
	}
	
	//求文件大小
	public static void size(int s) throws FileNotFoundException{
		if(s==1){
			FileInputStream fis01 = new FileInputStream("E:\\b.txt");
			fc = fis01.getChannel();
		}else{
			FileInputStream fis01 = new FileInputStream("E:\\c.txt");
			fc = fis01.getChannel();
		}
		
	}

}

本文地址:https://blog.csdn.net/xiaomingpeng/article/details/109928318

相关标签: java 加密解密