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

📄 javades.txt

📁 用java实现的加密算法
💻 TXT
📖 第 1 页 / 共 3 页
字号:
				for (int k = 0; k<48;k++) { k14[k]=kn[k]; }
				break;
			case 14:
				for (int k = 0; k<48;k++) { k15[k]=kn[k]; }
				break;
			case 15:
				for (int k = 0; k<48;k++) { k16[k]=kn[k]; }
				break;
		}
	}
  }
}


package des;

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

public class trying1{
	private String srcFileName;
	private String destFileName;
	private File srcFile;
	private File destFile;

	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(){
		FileOutputStream outputFile=null;
		try {
			outputFile=new FileOutputStream(srcFile,true);
			//outputFile.
			//System.out.println ("File stream creat successfully.");
	    }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)36);
				bufTemp.flip();			
				outChannel.position(outChannel.size());
				outChannel.write(bufTemp);
				//outChannel.
		    	bufTemp.clear();		
			}
		}catch(Exception ex){
			
		}
		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);
			//outputFile.
			//System.out.println ("File stream creat successfully.");
	    }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();			
			    outBuf.clear();
			    if (srcStr.length()==4){
			    	for (int i = 0; i<4; i++) {
	    				outBuf.putChar(srcStr.charAt(i));
	    			}
			    	outBuf.flip();
			    }else{
			    	outBuf.position(0);
			    	outBuf.limit(2*srcStr.length());
			    	for (int i = 0; i<srcStr.length(); i++) {
	    				outBuf.putChar(srcStr.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 trying1(String srcFileName,String destFileName){
		this.srcFileName=srcFileName;
		this.destFileName=destFileName;		
		analyzePath();
		srcFile=new File(srcFileName);
		destFile=new File(destFileName);
	}
	
	public static void main(String [] args){
		trying1 try1=new trying1("D:/信息安全/des1/trying.java","d:/1111.java");
		try1.file();
	}
}


package des;
//该类只是实验unicode编码格式的一个类,该类并没有被其他类引用。
public class trying{
	private String src;
	private byte[] key=new byte[64];
	
	public trying(String src){
		this.src=src;
	}
	public void op(){
		int j;
		for (int i = 0; i<4; i++) {
	  		j=src.charAt(i);
	  		System.out.println (j);
	  		key[16*i+0]=(byte)((j/32768)%2);
	  		key[16*i+1]=(byte)((j/16384)%2);
	  		key[16*i+2]=(byte)((j/8192)%2);
	  		key[16*i+3]=(byte)((j/4096)%2);
	  		key[16*i+4]=(byte)((j/2048)%2);
	  		key[16*i+5]=(byte)((j/1024)%2);
	  		key[16*i+6]=(byte)((j/512)%2);
	  		key[16*i+7]=(byte)((j/256)%2);
	  		key[16*i+8]=(byte)((j/128)%2);
	  		key[16*i+9]=(byte)((j/64)%2);
	  		key[16*i+10]=(byte)((j/32)%2);
	  		key[16*i+11]=(byte)((j/16)%2);
	  		key[16*i+12]=(byte)((j/8)%2);
	  		key[16*i+13]=(byte)((j/4)%2);
	  		key[16*i+14]=(byte)((j/2)%2);
	  		key[16*i+15]=(byte)(j%2);
	  				
		}
		for (int i = 0; i<64; i++) {
			if(i%4==0) System.out.print(" ");
			if(i%16==0) System.out.println ();
			System.out.print(key[i]);
			
	    }
	}
	public void op1(){
		int j;
		char ch;
		String str=null;
		StringBuffer strbuf=new StringBuffer();
		for (int i = 0; i<4; i++) {
			j=0;
			j=32768*key[16*i+0]+16384*key[16*i+1]+8192*key[16*i+2]+4096*key[16*i+3]+
			  2048*key[16*i+4]+1024*key[16*i+5]+512*key[16*i+6]+256*key[16*i+7]+
			  128*key[16*i+8]+64*key[16*i+9]+32*key[16*i+10]+16*key[16*i+11]+
			  8*key[16*i+12]+4*key[16*i+13]+2*key[16*i+14]+key[16*i+15];
			 ch=(char)j;
			 strbuf.append(ch);
			 
	    }
	    System.out.println (strbuf.toString());
	}
	/*
	public static void main(String[] args){
		trying t=new trying("我很AB");
		//trying t=new trying("ABCD");
		t.op();
		System.out.println ();
		t.op1();
	}
	*/
}

package des;

public class Des {
  /* Table - s1 */
  protected static byte[][] s1 = {
  {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
  {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
  {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
  {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 }};

  /* Table - s2 */
  protected static byte[][] s2 = {
  {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
  {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
  {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
  {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 }};

  /* Table - s3 */
  protected static byte[][] s3= {
  {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
  {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
  {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
  {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }};

  /* Table - s4 */
  protected static byte[][] s4 = {
  {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
  {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
  {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
  {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 }};

  /* Table - s5 */
  protected static byte[][] s5 = {
  {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
  {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
  {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
  {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }};

  /* Table - s6 */
  protected static byte[][] s6 = {
  {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
  {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
  {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
  {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }};

  /* Table - s7 */
  protected static byte[][] s7 = {
  {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
  {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
  {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
  {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}};

  /* Table - s8 */
  protected static byte[][] s8 = {
  {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
  {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
  {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
  {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}};
  //用于 S盒查找以确定对应的4位二进制数据#
  protected static byte[] binary={
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 
	0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 
	1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 
	1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 };
  
  //存储将要加密的明文或将要解密的密文  
  protected byte[] workData=new byte[64];
  protected String dataSrc;
  protected String dataDest;
  /*实验性数据
  protected static byte[] workData={
  0,0,0,0,0,0,0,1, 0,0,1,0,0,0,1,1, 0,1,0,0,0,1,0,1, 0,1,1,0,0,1,1,1,
  1,0,0,0,1,0,0,1, 1,0,1,0,1,0,1,1, 1,1,0,0,1,1,0,1, 1,1,1,0,1,1,1,1
  };
  */
  //用于数据变换中转
  protected byte[] bufout=new byte[64];
  //最终生成的密文或解密所得的明文
  protected byte[] output=new byte[64];
  protected byte[] Ln=new byte[32];
  protected byte[] Rn=new byte[32];
  protected byte[] LR=new byte[32];
  protected byte[] ER=new byte[48];
  protected byte[] temp=new byte[32];
  protected byte[] result=new byte[8];
  protected byte[] ki;
  protected String key;
  protected int readLen;
  protected boolean encFlag;   //1为加密,0为解密
  protected SubKey subKey;
  

  protected void convert4CharTo64bit(String data){
  	for (int i = 0; i<64; i++) {
  		workData[i]=0;
	}
	/* Convert from 4-char(coded in unicode) data into 64-bit data */ 

	for (int i = 0; i < readLen; i++) { 
		int j=data.charAt(i);
		workData[16*i+0]=(byte)((j/32768)%2);
	  	workData[16*i+1]=(byte)((j/16384)%2);
	  	workData[16*i+2]=(byte)((j/8192)%2);
	  	workData[16*i+3]=(byte)((j/4096)%2);
	  	workData[16*i+4]=(byte)((j/2048)%2);
	  	workData[16*i+5]=(byte)((j/1024)%2);
	  	workData[16*i+6]=(byte)((j/512)%2);
	  	workData[16*i+7]=(byte)((j/256)%2);
	  	workData[16*i+8]=(byte)((j/128)%2);
	  	workData[16*i+9]=(byte)((j/64)%2);
	  	workData[16*i+10]=(byte)((j/32)%2);
	  	workData[16*i+11]=(byte)((j/16)%2);
	  	workData[16*i+12]=(byte)((j/8)%2);
	  	workData[16*i+13]=(byte)((j/4)%2);
	  	workData[16*i+14]=(byte)((j/2)%2);
	  	workData[16*i+15]=(byte)(j%2);
	 } 
  }

  protected void convert64bitTo4Char(){		
	int j;
	char ch;
	StringBuffer strbuf=new StringBuffer();
	for (int i = 0; i<4; i++) {
		j=0;
		j=32768*output[16*i+0]+16384*output[16*i+1]+8192*output[16*i+2]+4096*output[16*i+3]+
		2048*output[16*i+4]+1024*output[16*i+5]+512*output[16*i+6]+256*output[16*i+7]+
		128*output[16*i+8]+64*output[16*i+9]+32*output[16*i+10]+16*output[16*i+11]+
		8*output[16*i+12]+4*output[16*i+13]+2*output[16*i+14]+output[16*i+15];
		ch=(char)j;	
		strbuf.append(ch);			 
	}
	dataDest=strbuf.toString();
}

  protected void IP(){
  	/* Initial Permutation of Data */ 
	bufout[ 0] = workData[57]; 
	bufout[ 1] = workData[49]; 
	bufout[ 2] = workData[41]; 
	bufout[ 3] = workData[33]; 
	bufout[ 4] = workData[25]; 
	bufout[ 5] = workData[17]; 
	bufout[ 6] = workData[ 9]; 
	bufout[ 7] = workData[ 1]; 
	bufout[ 8] = workData[59]; 
	bufout[ 9] = workData[51]; 
	bufout[10] = workData[43]; 
	bufout[11] = workData[35]; 
	bufout[12] = workData[27]; 
	bufout[13] = workData[19]; 
	bufout[14] = workData[11]; 
	bufout[15] = workData[ 3]; 
	bufout[16] = workData[61]; 
	bufout[17] = workData[53]; 
	bufout[18] = workData[45]; 
	bufout[19] = workData[37]; 
	bufout[20] = workData[29]; 
	bufout[21] = workData[21]; 
	bufout[22] = workData[13]; 
	bufout[23] = workData[ 5]; 
	bufout[24] = workData[63]; 
	bufout[25] = workData[55]; 

⌨️ 快捷键说明

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