⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedes.java

📁 JAVA版的DES加密算法
💻 JAVA
字号:
package com.trilobite.des;

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;

public class FileDES{
	private static final boolean enc=true;    //加密
	private static final boolean dec=false;	  //解密
	
	private String srcFileName; //源文件名
	private String destFileName; //目标文件名
	private String inKey;       //密钥
	@SuppressWarnings("unused")
	private boolean actionType; //判断是加密还是解密
	private File srcFile;//源文件
	private File destFile;	//目标文件
	private Des des;
	
	//分析文件路径,如果该目录下文件没有就生成一个
	private void analyzePath(){
		String dirName;
		int pos=srcFileName.lastIndexOf("/");
		//得到目录
		dirName=srcFileName.substring(0,pos);
		File dir=new File(dirName);
		if (!dir.exists()){
			System.err.println(dirName+" is not exist");
			System.exit(1);
		}else if(!dir.isDirectory()){
			System.err.println(dirName+" is not a directory");
			System.exit(1);
		}
		
		pos=destFileName.lastIndexOf("/");
		dirName=destFileName.substring(0,pos);
		dir=new File(dirName);
		if (!dir.exists()){
			if(!dir.mkdirs()){
				System.out.println ("can not creat directory:"+dirName);
				System.exit(1);
			}
		}else if(!dir.isDirectory()){
			System.err.println(dirName+" is not a directory");
			System.exit(1);
		}
	}
	
	private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{
		long byteLeft=channel.size()-channel.position();
		if(byteLeft==0L)
			return -1;
		buf.position(0);
		buf.limit(buf.position()+(byteLeft<8 ? (int)byteLeft :8));
		return channel.read(buf);
	}
	
	private void file_operate(boolean flag){
		des=new Des(inKey);
		FileOutputStream outputFile=null;
		try {
			outputFile=new FileOutputStream(srcFile,true);//创建对象,后一个参数如果为:true就写在后面,否则从头开始
	    }catch (java.io.FileNotFoundException e) {
	    	e.printStackTrace(System.err);
	    }
	    //为文件的操作设置一个通道
	    FileChannel outChannel=outputFile.getChannel();
	    
	    try{
		    if(outChannel.size()%2!=0){
				ByteBuffer bufTemp=ByteBuffer.allocate(1);//分配一个新的字节缓冲区
				bufTemp.put((byte)32);//将给定的值放入缓冲区
				bufTemp.flip();	//反转这个缓冲区		
				outChannel.position(outChannel.size());
				outChannel.write(bufTemp);//写入缓冲区域里面的数据
		    	bufTemp.clear();		
			}
		}catch(Exception ex){
			ex.printStackTrace(System.err);
			System.exit(1);
		}
		FileInputStream inFile=null;
		try{
			inFile=new FileInputStream(srcFile);
		}catch(java.io.FileNotFoundException e){
			e.printStackTrace(System.err);
			//System.exit(1);
		}
		outputFile=null;
		try {
			outputFile=new FileOutputStream(destFile,true);
	    }catch (java.io.FileNotFoundException e) {
	    	e.printStackTrace(System.err);
	    }
	    
	    FileChannel inChannel=inFile.getChannel();
		outChannel=outputFile.getChannel();
		
		
		ByteBuffer inBuf=ByteBuffer.allocate(8);
		ByteBuffer outBuf=ByteBuffer.allocate(8);
		
		try{
			String srcStr; //源字符串
			String destStr; //目标字符串
			while(true){
			
				if (replenish(inChannel,inBuf)==-1) break;
				//通过文件操作从文件中得到源文件
				srcStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();
				inBuf.clear();
				if (flag)
					destStr=des.enc(srcStr,srcStr.length());//解密操作
				else
					destStr=des.dec(srcStr,srcStr.length());//加密操作
			    outBuf.clear();
			    if (destStr.length()==4){
			    	for (int i = 0; i<4; i++) {
	    				outBuf.putChar(destStr.charAt(i));
	    			}
			    	outBuf.flip();
			    }else{
			    	outBuf.position(0);//设置测缓冲区的位置
			    	outBuf.limit(2*destStr.length());
			    	for (int i = 0; i<destStr.length(); i++) {
	    				outBuf.putChar(destStr.charAt(i));
	    			}
			    	outBuf.flip();
			    }
			    
			    try {
	    			outChannel.write(outBuf);
	    			outBuf.clear();
	    		}catch (java.io.IOException ex) {
	    			ex.printStackTrace(System.err);
	    		}
			}
			System.out.println (inChannel.size());
	    	System.out.println (outChannel.size());
			System.out.println ("EoF reached.");
			inFile.close();
			outputFile.close();
		}catch(java.io.IOException e){
			e.printStackTrace(System.err);
			System.exit(1);
		}	
	}

	public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){
		this.srcFileName=srcFileName;
		this.destFileName=destFileName;		
		this.actionType=actionType;
		analyzePath();
		srcFile=new File(srcFileName);
		destFile=new File(destFileName);		
		this.inKey=inKey;
		if (actionType==enc)
			file_operate(enc);
		else
			file_operate(dec);
	}
	
	
	public static void main(String[] args){
		String file1=System.getProperty("user.dir")+"/111.doc";
		String file2=System.getProperty("user.dir")+"/222.doc";
		String file3=System.getProperty("user.dir")+"/333.doc";
		String passWord="1234ABCD";
		//加密操作
		new FileDES(file1,file2,passWord,true);
		//解密操作
		new FileDES(file2,file3,passWord,false);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -