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

📄 des1.java

📁 DES RSA MD5 实现身份认证
💻 JAVA
字号:


import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class des1 {


		
		public static short [] bittest={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,-32768};

		public static int [] IP={ 58,50,42,34,26,18,10,2,			60,52,44,36,28,20,12,4,
									62,54,46,38,30,22,14,6,			64,56,48,40,32,24,16,8,
									57,49,41,33,25,17,9,1,			59,51,43,35,27,19,11,3,
									61,53,45,37,29,21,13,5,			63,55,47,39,31,23,15,7},

			 IP_1={ 40,8,48,16,56,24,64,32,			39,7,47,15,55,23,63,31,
					38,6,46,14,54,22,62,30,			37,5,45,13,53,21,61,29,
					36,4,44,12,52,20,60 ,28,		35,3,43,11,51,19,59,27,
					34,2,42,10,50,18,58,26,			33,1,41,9,49,17,57,25},

		 Extend={	32,1,2,3,4,5,			4,5,6,7,8,9,		8,9,10,11,12,13,
					12,13,14,15,16,17,		16,17,18,19,20,21,	20,21,22,23,24,25,
					24,25,26,27,28,29,		28,29,30,31,32,1},

		P={16,7,20,21,	29,12,28,17,	1,15,23,26,		5,18,31,10,
				2,8,24,14,		32,27,3,9,		19,13,30,6,		22,11,4,25};


		public static int [][][] S={
			{
				{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}
			},	//S1
			{
				{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}
			},	//S2
			{
				{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}
			},	//S3
			{
				{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}
			},	//S4
			{
				{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}
			},	//S5
			{
				{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}
			},	//S6
			{
				{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}
			},	//S7
			{
				{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}
			}	//S8
		};

		public static int [] PC_1={	57,	49,	41,	33,	25,	17,	9,		1,	58,	50,	42,	34,	26,	18,
						10,	2,	59,	51,	43,	35,	27,		19,	11,	3,	60,	52,	44,	36,
						63,	55,	47,	39,	31,	23,	15,		7,	62,	54,	46,	38,	30,	22,
						14,	6,	61,	53,	45,	37,	29,		21,	13,	5,	28,	20,	12,	4},

		 PC_2={	14,	17,	11,	24,	1,	5,		3,	28,	15,	6,	21,	10,
						23,	19,	12,	4,	26,	8,		16,	7,	27,	20,	13,	2,
						41,	52,	31,	37,	47,	55,		30,	40,	51,	45,	33,	48,
						44,	49,	39,	56,	34,	53,		46,	42,	50,	36,	29,	32};

		public static int [] Keymove={1,2,4,6,	8,10,12,14,	15,17,19,21,	23,25,27,28};
		
		
		public static short [] keyword;//初始化key
		
		public static String mingword,screatword,k="abcd";
		
		public static void main(String[] args)throws Exception  {
			des1 test=new des1();
			test.cbcja();
			test.cbcje();
		}
		
		
		public des1()
		{
			 keyword=new short [4];
			 int i=0;
			 if(k.length()>4)
			 {
				 for(i=0;i<4;i++) keyword[i]=(short)k.charAt(i);
			 }
			 else 
			 {
				 for(i=0;i<k.length();i++) keyword[i]=(short)k.charAt(i);
			 }
			 
			 key();

		}	

				
		private void cbcja()throws Exception {
			File file = new File("in.txt");
			FileInputStream in = new FileInputStream(file);
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] tmpbuf = new byte[1024];
			int count = 0;
			
			while ((count = in.read(tmpbuf)) != -1) {
				bout.write(tmpbuf, 0, count);
				tmpbuf = new byte[1024];
			}
			in.close();
			
			mingword=bout.toString();
			
			
			short [] temp_ming=new short [4];//每次输入64BIT的明文
			String ming_string="";//最后作为输出的密文字符
			short [] ming_short;//保存明文的数组
			int leng;//SHORT数组长度

			
			int i=mingword.length()/4;
			if((mingword.length()%4)!=0)
			{
				leng=i*4+4;
			ming_short=new short[leng];
			int t=0;
			for(;t<mingword.length();t++)
				ming_short[t]=(short)mingword.charAt(t);
			}
			else 
				{
				leng=i*4;
				ming_short=new short [leng];
				int t=0;
				for(;t<mingword.length();t++)
					ming_short[t]=(short)mingword.charAt(t);
				}
			                            
			   
			                              
			for(i=0;i<leng;)
			{
				temp_ming[0]=ming_short[i];//选取64bit
				temp_ming[1]=ming_short[i+1];
				temp_ming[2]=ming_short[i+2];
				temp_ming[3]=ming_short[i+3];
				i=i+4;
				int x;
				ming(temp_ming);//每64bit初始化一次
				for(x=0;x<16;x++)
				{
					ming_make(x);
					
				}
				short [] af_16;
				af_16=after_16();
				ming_string=ming_string+(char)af_16[0]+(char)af_16[1]+(char)af_16[2]+(char)af_16[3];
			}
			//screatword=ming_string;

			/*System.out.println(ming_string);
			BufferedWriter output=new BufferedWriter(new FileWriter("out.txt"));
			output.write(ming_string, 0, ming_string.length());
			output.close();*/
		
	        FileOutputStream fos = new FileOutputStream("out.txt");
	        ObjectOutputStream oos = new ObjectOutputStream(fos);

	        oos.writeObject(ming_string);

	        oos.close();

			
		}
		
		private void cbcje()throws Exception {
			
			/*BufferedReader inre=new BufferedReader(new FileReader("out.txt"));
			String s1=new String();
			while((s1=inre.readLine())!=null)
				screatword+=s1;
				
			System.out.println(screatword);
			
			File file = new File("out.txt");
			FileInputStream in = new FileInputStream(file);
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			byte[] tmpbuf = new byte[1024];
			int count = 0;
			
			while ((count = in.read(tmpbuf)) != -1) {
				bout.write(tmpbuf, 0, count);
				tmpbuf = new byte[1024];
			}
			in.close();
			screatword=bout.toString("UTF-8");*/
	        FileInputStream fis = new FileInputStream("out.txt");
	        ObjectInputStream ois = new ObjectInputStream(fis);

	        screatword= (String) ois.readObject();


	        ois.close();



	
			
			short [] temp_s=new short [4];
			 int i;                           
			  String screat_string="";
			  
			  short [] screat=new short[screatword.length()];
				for(i=0;i<screatword.length();i++) {
					screat[i]=(short)screatword.charAt(i);
				}    
		
				//System.out.println(screatword);
				
			for(i=0;i< screat.length;)
			{
				temp_s[0]=screat[i];//选取64bit
				temp_s[1]=screat[i+1];
				temp_s[2]=screat[i+2];
				temp_s[3]=screat[i+3];
				i=i+4;
				int x;
				ming(temp_s);//每64bit初始化一次
				for(x=15;x>=0;x--)
				{
					ming_make(x);
					
				}
				short [] af_16;
				af_16=after_16();
				screat_string=screat_string+(char)af_16[0]+(char)af_16[1]+(char)af_16[2]+(char)af_16[3];
			}
			
			System.out.println(screat_string);
			BufferedWriter output=new BufferedWriter(new FileWriter("desout.txt"));
			output.write(screat_string, 0, screat_string.length());
			output.close();
			
		}
		

		
		
		//把字符串对应的每位0或1状态反映到数组bit里
		public static int[] changebit(short [] income,int size) {
			int i=0;
			int [] state=new int[size];
			for(;i<size;i++) 
			{
				if(testbit(income[i/16],bittest[i%16])==true) state[i]=1;
				else state[i]=0;
			}
			
			return state;
		}

		//测试字符某一位是0还是1
		private static boolean testbit(short c,int testnum) {
			if((c&testnum)==0)  return false;
			else return true;
		}
		
		
		//根据所给位状态组装到字符里
		public static short[] shortmaker(int [] state,int size) {
			short [] c=new short[size/16];
			int i=0;
			for(;i<=size-1;i++)
			{
				if(state[i]==1)
				c[i/16]=(short)((c[(i/16)])|(bittest[(i%16)]));
			}
			return c;
		}
		
		
		
		//置换扩展
		public static int[] ip_extend(int [] state,int [] list,int size)
		{
			int i=0;
			int [] temp=new int [size];
			for(;i<size;i++)
			{
				temp[i]=state[list[i]-1];//扩展置换表从1开始,所以要减1
			}
			
			return temp;
		}
		
		
		//S盒选择:输入Short数组,返回32比特的状态
		public static int[] Sbox(short [] income) {
			int [] S_bitState=new int [48];
			int [] Sbit=new int [32];
			S_bitState=changebit(income,48);
			int i=0,row,column,temp;
			for(;i<8;i++)
			{
				row=row(i,S_bitState);
				column=column(i,S_bitState);
				temp=S[i][row][(column)];
				Sbit[i*4+3]=temp%2;
				Sbit[i*4+2]=(temp/2)%2;
				Sbit[i*4+1]=(temp/4)%2;
				Sbit[i*4]=(temp/8)%2;
			}
			return Sbit;
		}
	
		//计算S盒中的行号
		private static int row(int i,int []S_bitState) {
			if((S_bitState[i*6]==0)&(S_bitState[i*6+5]==0)) return 0;
			else if((S_bitState[i*6]==0)&(S_bitState[i*6+5]==1)) return 1;
			else if((S_bitState[i*6]==1)&(S_bitState[i*6+5]==0)) return 2;
			else return 3;
		}
		
		//计算S盒中的列号
		private static int column(int i,int [] S_bitState) {
			int colunm=0,n=0;
			for(n=1;n<5;n++)
			{
				colunm*=2;
				if(S_bitState[i*6+n]==1)	colunm++;
			}
			return colunm;
		}
		
		//左循环移位
		private static int[] move(int [] income,int i)
		{
			int time=Keymove[i];
			int n=0;
			int [] temp=new int[28];
			for(;n<28;n++)
			temp[n]=income[(n+time)%28];
			return temp;
		}
		
		public static int []key_right =new int [28];
		public static int []key_left=new int [28];
		
		//将KEY分解出状态并转换分为左右两部分。
		public static void  key() {
			int [] keystate=new int [64];
			keystate=changebit(keyword,64);//分解为位状态
			keystate=ip_extend(keystate,PC_1,56);//置换
			int n=0;
			for(;n<28;n++)//分左右部分
			{
				key_left[n]=keystate[n];
				key_right[n]=keystate[n+28];
			}
		}
		
		
		//输入第一次产生的左右部分KEY和需要产生的第i轮生成SHORT数组状态的48位KEY
		public static short[]  keymaker(int i) {
			
			int [] key_right_temp =new int [28];
			int [] key_left_temp=new int [28];
			key_right_temp=move(key_right,i);//移位
			key_left_temp=move(key_left,i);//移位
			int [] temp=new int[56];
			int [] temp2=new int [48];
			int n=0;
			for(;n<28;n++)//移位后组合为56BIT
				{
				temp[n]=key_left_temp[n];				
				temp[28+n]=key_right_temp[n];
				}
			
			temp2=ip_extend(temp,PC_2,48);//转换为48BIT
			
			short [] Kshort=new short [3];
			Kshort=shortmaker(temp2,48);//整合成为SHORT数组
			
			return Kshort;
	}
		

		
		public  static int [] m_right_bit =new int [32];
		public  static int [] m_left_bit =new int [32];
		
		public static short [] rightm=new short[2];		
		public static short [] leftm=new short[2];
		
		
		//分解输入的明文,并分为左右部分状态
		public static void ming(short []income) {
			int [] temp=new int [64];
			
			temp=changebit(income,64);// 转换为位状态
			temp=ip_extend(temp,IP,64);//IP转换
			
			int i=0;//分左右部分
			for(;i<32;i++) {
				m_left_bit[i]=temp[i];
				m_right_bit[i]=temp[32+i];
			}		
			
			rightm=shortmaker(m_right_bit,32);
			leftm=shortmaker(m_left_bit,32);
			
		}

		
		//生成每轮的左右密文short数组(修改rigntm,leftm)
		public  void ming_make(int time) {
			short [] temp=new short[2];

			
			short [] lefttemp=new short[2];//备份LEFTM
			lefttemp[0]=leftm[0];
			lefttemp[1]=leftm[1];
			
			
			//leftm=shortmaker(temp,m_right_bit,32);//右变左产生下一次的左部
			leftm[0]=rightm[0];
			leftm[1]=rightm[1];
			
			
			int [] ex=new int[48];
			int [] sbit =new int [32];
			short [] exshort=new short[3];
			
			ex=ip_extend(m_right_bit,Extend,48);//扩展
			
			exshort=shortmaker(ex,48);//转换为Shotr数组方便异或
			int i=0;
			for(;i<3;i++)//与生成的KEY异或
				exshort[i]=(short)(exshort[i]^keymaker(time)[i]);
			
			
			sbit=Sbox(exshort);//s盒选择
			sbit=ip_extend(sbit,P,32);//P置换
			temp=shortmaker(sbit,32);//整合为SHORT数组
			for(i=0;i<2;i++)//与左部分异或产生下一轮的右部
				rightm[i]=(short)(lefttemp[i]^temp[i]);
			
		//更变左右部分的位状态数组
			m_left_bit=changebit(leftm,32);
			m_right_bit=changebit(rightm,32);
		}
		
		
		public short[] after_16() {

			int i=0;
			int [] state=new int [64];
			for(;i<32;i++)
			{
				state[i]=m_right_bit[i];
				state[i+32]=m_left_bit[i];
			}
			state=ip_extend(state,IP_1,64);
			
			short [] ming=new short[4];
			ming=shortmaker(state,64);
			return ming;
			
		}

	}
		
	

⌨️ 快捷键说明

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