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

📄 rijndaeltest.java

📁 这个RIJNDAEL的算法是用来产生密文序列JAVA算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class RijndaelTest
{
	public static void main(String[] args)
	{
		RijndaelFrame RF=new RijndaelFrame();
		RF.setTitle("RijndaelTest");
		RF.setVisible(true);
		RF.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

}
class RijndaelFrame extends JFrame
{
	private static int DEFAULT_WIDTH=600;
	private static int DEFAULT_HEIGHT=450;
	private static int DEFAULT_TOP=80;
	private static int DEFAULT_LEFT=75;
	final JTextArea ta1=new JTextArea(30,20);
	final JTextArea ta2=new JTextArea(30,20);
	final JTextField keyfield=new JTextField(32);
	

	int keylen=8;
	
	int[] reout=new int[16];
	int[] deout=new int[16];
	int[] key={0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c};
	int[] keyexpl4={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
	//int[] key6={0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b};
	//int[] key8={0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
			    //0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4};
    int[] input={0x32,0x43,0xf6,0xa8,0x88,0x5a,0x30,0x8d,0x31,0x31,0x98,0xa2,0xe0,0x37,0x07,0x34};
    int[] inputexpl={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
    
    int[] keyexpl6={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17};
    int[] keyexpl8={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
    		        0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f};
	public RijndaelFrame()
	{
		setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
		setLocation(DEFAULT_LEFT,DEFAULT_TOP);
	
		final int[] keys=new int[4*keylen];
		JLabel label1=new JLabel("INPUT");
		JLabel label2=new JLabel("OUTPUT");
		final JTextField tipmsg=new JTextField(20);
		tipmsg.setBackground(Color.red);
		tipmsg.enable(false);
		JPanel p=new JPanel();
		p.add(label1);
		p.add(tipmsg);
		p.add(label2);
		//p.setLayout(new GridLayout(1,2));
		add(p,BorderLayout.NORTH);
		
		ta1.setBackground(Color.green);
		
		ta2.setBackground(Color.orange);
		
		JScrollPane pane1=new JScrollPane(ta1);
		JScrollPane pane2=new JScrollPane(ta2);
		JPanel panel1=new JPanel();
		panel1.add(pane1);
		panel1.add(pane2);
		add(panel1,BorderLayout.CENTER);
		
		JButton Encrypt,Decrypt,二次加密,demostrate,clear_all,二次解密,write;
		JPanel panel=new JPanel();
		Encrypt=new JButton("Encrypt");
		Decrypt=new JButton("Decrypt");
		二次加密=new JButton("二次加密");
		demostrate=new JButton("demostrate");
		clear_all=new JButton("clear all ");
		二次解密=new JButton("二次解密");
		write=new JButton("write");
		
		write.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e) 
			{
				//File f=new File("rijndael.dat");
				
				//for(int i=0;i<200;i++)
				////String str="";
					
			///	    reout=encipher(reout,keyexp.keysched(keys,keylen));
					/*for(int j=0;j<reout.length;j++)
			         {
			        	 String temp1="";
				        	
			        	 //temp1=Integer.toHexString(out[j][i]);
			             //if(out[j][i]<16)
			     		 //str=str+"0";
			        	 temp1=Integer.toBinaryString(reout[j]);
			        	 if(reout[j]<128&&reout[j]>=64)
			     		     str=str+"0";
			        	 if(reout[j]<64&&reout[j]>=32)
			         		 str=str+"00";
			        	 if(reout[j]<32&&reout[j]>=16)
			         		 str=str+"000";
			        	 if(reout[j]<16&&reout[j]>=8)
			         		 str=str+"0000";
			        	 if(reout[j]<8&&reout[j]>=4)
			         		 str=str+"00000";
			        	 if(reout[j]<4&&reout[j]>=2)
			         		 str=str+"000000";
			        	 if(reout[j]<2)
			         		 str=str+"0000000";
				         str=str+temp1;
				         
			         }
					try{
						FileWriter fos=new FileWriter(f,true);
						fos.write(str);
						fos.flush();
						}
						catch(Exception ex)
						{
							
						}*/
				//}
				String strout="";
				double lon=power(2,64)-1;
				for(int k=0;k<100;k++)
				{	
				reout=encipher(reout,keyexp.keysched(keys,keylen));
				double lon1=0.0,lon2=0.0;
				//String str=ta2.getText()+"\n";
				String str="";
				for(int j=0;j<16;j++)
				{
					String temp1="";
					temp1=Integer.toBinaryString(reout[j]);
		        	 if(reout[j]<128&&reout[j]>=64)
		     		     str=str+"0";
		        	 if(reout[j]<64&&reout[j]>=32)
		         		 str=str+"00";
		        	 if(reout[j]<32&&reout[j]>=16)
		         		 str=str+"000";
		        	 if(reout[j]<16&&reout[j]>=8)
		         		 str=str+"0000";
		        	 if(reout[j]<8&&reout[j]>=4)
		         		 str=str+"00000";
		        	 if(reout[j]<4&&reout[j]>=2)
		         		 str=str+"000000";
		        	 if(reout[j]<2)
		         		 str=str+"0000000";
			         str=str+temp1;	 
				}
				//ta2.setText(str);
				for(int i=63;i>=0;i--)
				{
					lon1=lon1+((int)str.charAt(i)-(int)'0')*power(2,i);
				}
				for(int i=127;i>=64;i--)
				{
					lon2=lon2+((int)str.charAt(i)-(int)'0')*power(2,i-64);
				}
				Double Lon1=new Double((double)lon1/lon);
				Double Lon2=new Double((double)lon2/lon);
				//ta2.setText(ta2.getText()+"\n"+Loni.toString());
				strout=strout+Lon1.toString()/*+"\n"+Lon2.toString()*/+"\n";
				}
				ta2.setText(strout);
				
			}
		});
		
		clear_all.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				tipmsg.setText("");
				ta1.setText("");
				ta2.setText("");
				keyfield.setText("");
			}
		});
		
		demostrate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String str=null,str2=null;
				int[] tempkey=keyexpl4;
				switch(keylen)
				{
				case 4:tempkey=keyexpl4;
				break;
				case 6:tempkey=keyexpl6;
				break;
				case 8:tempkey=keyexpl8;
				break;
				default:
					break;
				}
				for(int i=0;i<16;i++)
				str=str+" "+Integer.toHexString(inputexpl[i]);
				for(int i=0;i<tempkey.length;i++)
					str2=str2+" "+Integer.toHexString(tempkey[i]);
				//System.out.println(str.length());
				str=str.substring(4,str.length());
				str2=str2.substring(4,str2.length());
				ta1.setText(str);
				keyfield.setText(str2);
				reout=encipher(inputexpl,keyexp.keysched(tempkey,keylen));
				print(reout);
				//reout=encipher(input,keyexp.keysched(key,4));
			}
		});
		
		二次加密.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(0==keyfield.getText().length())
					tipmsg.setText("please encipher first");
				else
					{
					reout=encipher(reout,keyexp.keysched(keys,keylen));
					print(reout);
					}
			}
		});
		二次解密.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(0==keyfield.getText().length())
					tipmsg.setText("please encipher first");
				else
					{
					reout=decipher(reout,keyexp.keysched(keys,keylen));
					print(reout);
					}
				
			}
		});
		Decrypt.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				
               // int[] deput={0x39,0x25,0x84,0x1d,0x02,0xdc,0x09,0xfb,0xdc,0x11,0x85,0x97,0x19,0x6a,0x0b,0x32};
				//reout=decipher(deput,keyexp.keysched(key,4));
                char[] keychar=keyfield.getText().toCharArray();
			    for(int i=0;i<keychar.length;i++)
					keys[i]=(int)keychar[i];
			    
				if(0==ta1.getText().length())
					tipmsg.setText("please enter INPUT");
				else if(0==keyfield.getText().length())
					tipmsg.setText("please enter keys");
				else
				{
				
					char[] plaintext=ta1.getText().toCharArray();
					int textlength=(plaintext.length/16+1)*16;
					
					int[] input1=new int[textlength];
					int i=0;
					for(i=0;i<plaintext.length;i++)
					{
					   input1[i%16]=(int)plaintext[i];
					   if(15==i%16)
					    {
						   reout=decipher(input1,keyexp.keysched(keys,keylen));
						   print(reout);
						   for(int k=0;k<16;k++)
							   input1[k]=0;
					    }
					}
					if(0!=i%16)
						reout=decipher(input1,keyexp.keysched(keys,keylen));
					print(reout);
				    
				}
			}
		});
		Encrypt.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				char[] keychar=keyfield.getText().toCharArray();
				//for(int i=0;i<keychar.length;i++)
					//keys[i]=(int)keychar[i];
				//int[] keyHex=toHex(keychar);
				
				if(0==ta1.getText().length())
					tipmsg.setText("please enter INPUT");
				else if(0==keyfield.getText().length())
					tipmsg.setText("please enter keys");
				else
				{
				char[] plaintext=ta1.getText().toCharArray();
				//int[] inputHex=toHex(plaintext);
				int textlength=(plaintext.length/16+1)*16;
				
				int[] input1=new int[textlength];
				int i=0;
				for(i=0;i<plaintext.length;i++)
				{
				   input1[i%16]=(int)plaintext[i];
				   if(15==i%16)
				    {
					   reout=encipher(input,keyexp.keysched(key,keylen));
					   print(reout);
					   for(int k=0;k<16;k++)
						   input1[k]=0;
				    }
				}
				if(0!=i%16)
					reout=encipher(input,keyexp.keysched(key,keylen));
				print(reout);
				}
			   // reout=encipher(input,keyexp.keysched(key,4));
				
				
				
			}
		});
		
		JLabel lab=new JLabel("enter keyword");
		final JComboBox keycomboBox=new JComboBox();
		keycomboBox.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				String str=(String)keycomboBox.getSelectedItem();
				if(str.equals("128"))
						keylen=4;
				else if(str.equals("192"))
					keylen=6;
				else if(str.equals("256"))
					keylen=8;
			}
		});
		keycomboBox.addItem("128");
		keycomboBox.addItem("192");
		keycomboBox.addItem("256");
		//panel.add(btn1);
		panel.add(lab);
		panel.add(keycomboBox);
		panel.add(keyfield);
		//panel.add(btn2);
		//panel.add(btn3);
		JPanel panl=new JPanel();
		panl.setLayout(new GridLayout(7,1,20,30));
		panl.add(Encrypt);
		panl.add(Decrypt);
		panl.add(二次加密);
		panl.add(二次解密);
		panl.add(demostrate);
		panl.add(clear_all);
		panl.add(write);
		///panel.setLayout(new GridLayout(1,4));
		add(panel,BorderLayout.SOUTH);
		add(panl,BorderLayout.EAST);
		pack();
		
		
	}
	
	
	
	void print(int[] out) 
	{
		String str=ta2.getText();
		for(int i=0;i<out.length;i++)
	
         for(int j=0;j<out.length;j++)
         {
        	 String temp1="";
	        	
        	 //temp1=Integer.toHexString(out[j][i]);
             //if(out[j][i]<16)
     		 //str=str+"0";
        	 temp1=Integer.toBinaryString(out[j]);
        	 if(out[j]<128&&out[j]>=64)
     		     str=str+"0";
        	 if(out[j]<64&&out[j]>=32)
         		 str=str+"00";
        	 if(out[j]<32&&out[j]>=16)
         		 str=str+"000";
        	 if(out[j]<16&&out[j]>=8)
         		 str=str+"0000";
        	 if(out[j]<8&&out[j]>=4)
         		 str=str+"00000";
        	 if(out[j]<4&&out[j]>=2)
         		 str=str+"000000";
        	 if(out[j]<2)
         		 str=str+"0000000";
	         str=str+temp1;
	         
         }
    //str=str.substring(0, str.length()-7);
    //str=str+"\n";
    ta2.setText(str);
	}
	static int[] toHex(char[] ch)
	{

⌨️ 快捷键说明

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