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

📄 filedes.java

📁 关于JAVA对DES算法的实现.希望大家能多交流,能提宝贵意见
💻 JAVA
字号:
import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
public class FileDES
{       
   private static boolean ture;
   private static final boolean enc=ture;
   private static final boolean dec=false;
   private String sourceFileName;
   private String targetFileName;
   private String inKey;
   private boolean encdes ;
   private File sourceFile;
   private File targetFile;
   private Des des;
   private void getpath()
   {           
       String pathname;
       int pos=sourceFileName.lastIndexOf("/");
       pathname=sourceFileName.substring(0,pos);
       File dir=new File(pathname);
       if(!dir.exists()){
                             System.err.println(pathname+" is not exist");
                             System.exit(1);
                         }else if(!dir.isDirectory()){
                           System.err.println(pathname+" is not a directory");
                           System.exit(1);
                         }
       pos=targetFileName.lastIndexOf("/");
       pathname=targetFileName.substring(0,pos);
       dir=new File(pathname);
       if (!dir.exists()){
                 if(!dir.mkdirs()){
                   System.out.println ("can not creat directory:"+pathname);
                  System.exit(1);
                 }
         }else if(!dir.isDirectory()){
                System.err.println(pathname+" is not a directory");
                System.exit(1);
       }
   }
   private static int bufcontent(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 fileAction(boolean flag)
   {
       des=new Des(inKey);
       FileOutputStream outputFile=null;
       try 
       { 
           outputFile=new FileOutputStream(sourceFile,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(sourceFile);
        }
        catch(java.io.FileNotFoundException e)
        {
        e.printStackTrace(System.err);
        }  
        outputFile=null;
        try 
        {
      outputFile=new FileOutputStream(targetFile,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 sourceStr;
                String targetStr;
                while(true)
                {
                      if (bufcontent(inChannel,inBuf)==-1) break;
                            sourceStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();
                            inBuf.clear();
                            if (flag)      
                              targetStr=des.enc(sourceStr,sourceStr.length());
                            else         
                              targetStr=des.dec(sourceStr,sourceStr.length());
                              outBuf.clear();
                            if (targetStr.length()==4)
                              {
                                  for  (int i = 0; i<4; i++) 
                                  {
                                      outBuf.putChar(targetStr.charAt(i));
                                  }
                                      outBuf.flip();                                                                      
                         }
                             else
                         {
                                  outBuf.position(0);
                                  outBuf.limit(2*targetStr.length());
                              for (int i =0; i<targetStr.length(); i++) 
                             { 
                                   outBuf.putChar(targetStr.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 sourceFilename,String targetFilename,String inkey,boolean encodes)
 {
	 this.sourceFileName = sourceFileName;
	 this.targetFileName = targetFileName;
	 this.encdes = encodes;
	 getpath();
	 sourceFile = new File(sourceFileName);
	 targetFile = new File(targetFileName);
	 this.inKey = inkey;
	 if(encodes ==enc)
		 fileAction(enc);
	 else
		 fileAction(dec);
 }
  public static void  main(String[] args)
  {
	   String  srcfile=System.getProperty("user.dir")+"/soucefile.doc";
	   String cypfile=System.getProperty("user.dir")+"/cryptfile.doc";
	   String  trgfile=System.getProperty("user.dir")+"/targetfile.doc";
	   String  passWord="ZYWX1234";
	   new FileDES(srcfile,cypfile,passWord,true);
	   new FileDES(cypfile,trgfile,passWord,false);
  }
}

⌨️ 快捷键说明

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