當前位置:編程學習大全網 - 編程語言 - 用java實現des算法

用java實現des算法

包des

導入Java . io . *;

導入Java . nio . *;

導入Java . nio . channels . file channel;

公共類文件{

私有靜態最終布爾enc = true//加密

私有靜態最終布爾dec = false//解密

私有字符串srcFileName

私有字符串destFileName

私弦因基;

私有布爾actionType

私有文件srcFile

私有文件destFile

私人Des des

私有void analyzePath(){

字符串目錄名;

int pos = srcfilename . lastindexof("/");

dirName = src filename . substring(0,pos);

File dir =新文件(dirName);

如果(!目錄存在()){

system . err . println(dirName+" is not exist ");

system . exit(1);

}else if(!dir.isDirectory()){

System.err.println(dirName+"不是目錄");

system . exit(1);

}

pos = dest filename . lastindexof("/");

dirName = dest filename . substring(0,pos);

dir =新文件(dirName);

如果(!目錄存在()){

如果(!dir.mkdirs()){

System.out.println("無法創建目錄:"+dirName);

system . exit(1);

}

}else if(!dir.isDirectory()){

System.err.println(dirName+"不是目錄");

system . exit(1);

}

}

私有靜態int replacement(文件通道通道,字節緩沖區buf)拋出IOException{

long byte left = channel . size()-channel . position();

if(byteLeft==0L)

return-1;

buf . position(0);

buf . limit(buf . position()+(byte left & lt;8 ?(int)byte left:8));

返回channel . read(buf);

}

私有void file_operate(布爾標誌){

des =新des(因基);

file output stream output file = null;

嘗試{

output file = new file output stream(src file,true);

} catch(Java . io . file not found exception e){

e . printstacktrace(system . err);

}

file channel out channel = output file . get channel();

嘗試{

if(outChannel.size()%2!=0){

byte buffer buf temp = byte buffer . allocate(1);

bufTemp.put((字節)32);

buf temp . flip();

out channel . position(out channel . size());

out channel . write(buf temp);

buf temp . clear();

}

}catch(Exception ex){

ex . printstacktrace(system . err);

system . exit(1);

}

FileInputStream inFile = null

嘗試{

inFile = new file inputstream(src file);

} catch(Java . io . file not found exception e){

e . printstacktrace(system . err);

//system . exit(1);

}

outputFile = null

嘗試{

output file = new file output stream(dest file,true);

} catch(Java . io . file not found exception e){

e . printstacktrace(system . err);

}

file channel in channel = infile . get channel();

out channel = output file . get channel();

byte buffer in buf = byte buffer . allocate(8);

byte buffer out buf = byte buffer . allocate(8);

嘗試{

字符串srcStr

字符串destStr

while(true){

if(reply(in channel,in buf)=-1)break;

srcStr =((byte buffer)(in buf . flip()))。asCharBuffer()。toString();

inbuf . clear();

if(標誌)

destStr=des.enc(srcStr,srcStr . length());

其他

destStr=des.dec(srcStr,srcStr . length());

out buf . clear();

if (destStr.length()==4){

for(int I = 0;我& lt4;i++) {

out buf . putchar(deststr . charat(I));

}

out buf . flip();

}否則{

out ff . position(0);

out ff . limit(2 * deststr . length());

for(int I = 0;我& ltdeststr . length();i++) {

out buf . putchar(deststr . charat(I));

}

out buf . flip();

}

嘗試{

out channel . write(out buf);

out buf . clear();

}catch (java.io.IOException ex) {

ex . printstacktrace(system . err);

}

}

system . out . println(in channel . size());

system . out . println(out channel . size());

System.out.println ("EoF已達到。");

infile . close();

output file . close();

}catch(java.io.IOException e){

e . printstacktrace(system . err);

system . exit(1);

}

}

public FileDES(String src filename,String destFileName,String inKey,boolean actionType){

this.srcFileName = srcFileName

this . dest file name = dest file name;

this.actionType = actionType

analyze path();

srcFile =新文件(src filename);

destFile =新文件(dest File name);

this.inKey = inKey

if (actionType==enc)

file _ operate(enc);

其他

file _ operate(dec);

}

公共靜態void main(String[] args){

string file 1 = system . getproperty(" user . dir ")+"/111 . doc ";

string file 2 = system . getproperty(" user . dir ")+"/222 . doc ";

string file 3 = system . getproperty(" user . dir ")+"/333 . doc ";

string passWord = " 1234 ABCD ";

FileDES FileDES = new FileDES(file 1,file2,passWord,true);

FileDES FileDES 1 = new FileDES(file 2,file3,passWord,false);

}

  • 上一篇:java編程,兩個p進制數相乘
  • 下一篇:電氣自動化安裝與維修就業方向
  • copyright 2024編程學習大全網