DES加密解密

本文介绍了一个使用DES算法进行文件加密和解密的Java程序。该程序能够处理不同大小的文件,并确保加密后的文件可以被正确解密。通过实例演示了如何使用此程序对文件进行加密和解密操作。

DES加密解密
package 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;
 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);
     }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";
  FileDES fileDes=new FileDES(file1,file2,passWord,true);
  FileDES fileDes1=new FileDES(file2,file3,passWord,false);
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值