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

📄 aes.java

📁 用java编写的AES加密解密程序,程序需要同目录下有key.dat作为加密解密密钥,输出密文cipher.dat,输出解密文件descrypt.dat
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Arrays;

class Work
{
	static AESframe frame=new AESframe();
	static AES aes=new AES();
	public static void main(String []args)
	{
		frame.show();
	}
}
//AES加密/解密主窗口框架
class AESframe extends JFrame implements ActionListener
{
	static FileInputStream file=null;
	FileOutputStream tofile=null;
	FileInputStream keyfile=null;
	JMenuBar menu=new JMenuBar();
	JMenu menu_file=new JMenu("文件");
	JMenu menu_others=new JMenu("其他");
	JMenuItem file_open=new JMenuItem("打开");
	JMenuItem Encryption=new JMenuItem("文件加密");
	JMenuItem Decryption=new JMenuItem("文件解密");
	JMenuItem file_close=new JMenuItem("关闭");
	JMenuItem others_S=new JMenuItem("S盒");
	JMenuItem others_Sr=new JMenuItem("逆S盒");
	JMenuItem others_about=new JMenuItem("关于");
	JTextArea aestext=new JTextArea();
	JTextArea result_text=new JTextArea();
	JPanel pane=new JPanel();
	GridBagConstraints c=new GridBagConstraints();
	public AESframe()
	{
		super("AES加密解密");
		this.setSize(800,600);
		
		file_open.addActionListener(this);
		file_close.addActionListener(this);
		others_S.addActionListener(this);
		others_Sr.addActionListener(this);
		others_about.addActionListener(this);
		Encryption.addActionListener(this);
		Decryption.addActionListener(this);
		
		menu_file.add(file_open);
		menu_file.add(Encryption);
		menu_file.add(Decryption);
		menu_file.add(file_close);
		menu_others.add(others_S);
		menu_others.add(others_Sr);
		menu_others.add(others_about);
		menu.add(menu_file);
		menu.add(menu_others);
		this.setJMenuBar(menu);
		
		c.fill=c.BOTH;
		c.insets=new Insets(5,5,5,5);
		pane.setLayout(new GridBagLayout());
		aestext.setTabSize(4);
		result_text.setTabSize(4);
		c.weightx=c.weighty=1.0;
		c.gridwidth=4;c.gridheight=4;
		c.gridx=0;c.gridy=0;
		pane.add(new JScrollPane(aestext),c);
		c.gridx=0;c.gridy=5;c.gridwidth=c.gridheight=1;
		c.weightx=c.weighty=0.0;
		pane.add(new JLabel("运行结果"),c);
		c.gridx=0;c.gridy=6;c.gridwidth=4;c.gridheight=2;
		c.weightx=c.weighty=0.5;
		pane.add(new JScrollPane(result_text),c);
		this.add(pane);
		
	}
	public void actionPerformed(ActionEvent e)//按键事件
	{
		if(e.getSource().equals(file_close))
		{
			try
			{
				file.close();
				tofile.close();
			}catch(IOException ioe){}
			finally
			{
				this.dispose();
				System.exit(1);
			}
		}
		else if(e.getSource().equals(others_S))
			S();
		else if(e.getSource().equals(others_Sr))
			S_r();
		else if(e.getSource().equals(others_about))
		{
			AboutFrame about=new AboutFrame();
			about.show();
		}
		else if(e.getSource().equals(file_open))
		{
			OpenFrame open_frame=new OpenFrame();
			open_frame.show();
		}
		else if(e.getSource().equals(Encryption))
		{
			try
			{
				keyfile=new FileInputStream("key.dat");
				tofile=new FileOutputStream("cipher.dat");
			}catch(IOException ioe){}
			FileEncryption(file,tofile,keyfile);
		}
		else if(e.getSource().equals(Decryption))
		{
			try
			{
				keyfile=new FileInputStream("key.dat");
				tofile=new FileOutputStream("descrypt.dat");
			}catch(IOException ioe){}
			FileDecryption(file,tofile,keyfile);
		}
	}
	
	public void FileEncryption(FileInputStream f,FileOutputStream tf,FileInputStream kf)//文件加密
	{
		if(f==null||tf==null||kf==null)
			System.exit(1);
		int key[][]=new int [4][4];
		try
		{
			for(int i=0;i<4&&kf.available()>0;i++)
				for(int j=0;j<4&&kf.available()>0;j++)
					key[i][j]=kf.read();
		}catch(IOException ioe){}
		int len=0;
		try
		{
			len=f.available();
		}catch(IOException ioe){}
		int temp[]=new int [len];
		try
		{
			for(int i=0;i<temp.length;i++)
				temp[i]=f.read();
		}
		catch(IOException ioe)
		{
			System.exit(1);
		}
		int in[][]=new int [4][4];
		int out[][]=new int [4][4];
		result_text.setText("");
		for(int k=0;k<len/16;k++)
		{
			int m=0;
			for(int i=0;i<4;i++)
				for(int j=0;j<4;j++)
				{
					in[i][j]=temp[k*16+m];
					m++;
				}
			out = EncryptionProcess(in,key);
			try
			{
				for(int i=0;i<4;i++)
					for(int j=0;j<4;j++)
						tf.write((byte)out[i][j]);
			}catch(IOException ioe){}
		}
		if(len%16!=0)
		{
			int k=len-len%16;
			for(int i=0;i<4;i++)
				for(int j=0;j<4;j++)
					in[i][j]=0;
			for(int i=0;i<4&&k<len;i++)
				for(int j=0;j<4&&k<len;j++)
				{
					in[i][j]=temp[k];
					k++;
				}
			out=EncryptionProcess(in,key);
			try
			{
				for(int i=0;i<4;i++)
					for(int j=0;j<4;j++)
						tf.write((byte)out[i][j]);
			}catch(IOException ioe){}
		}
	}
	
	public int [][] EncryptionProcess(int [][]in,int [][]key)//加密
	{
		int w[][]=new int [44][4];
		w=AES.KeyExpansion(key,w);
		int temp[][]=AES.AddRoundKey(in,w,0);
		for(int i=1;i<10;i++)
			temp=AES.AddRoundKey(AES.MixColumns(AES.ShiftRows(AES.SubBytes(temp))),w,i);
		int out[][]=AES.AddRoundKey(AES.ShiftRows(AES.SubBytes(temp)),w,10);
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
				this.result_text.append(Integer.toHexString(out[i][j])+"\t");
			this.result_text.append("\n");
		}
		return out;
	}
	
	public void FileDecryption(FileInputStream f,FileOutputStream tf,FileInputStream kf)//文件解密
	{
		if(f==null||tf==null||kf==null)
			System.exit(1);
		int key[][]=new int [4][4];
		try
		{
			for(int i=0;i<4&&kf.available()>0;i++)
				for(int j=0;j<4&&kf.available()>0;j++)
					key[i][j]=kf.read();
		}catch(IOException ioe){}
		int len=0;
		try
		{
			len=f.available();
		}catch(IOException ioe){}
		int temp[]=new int [len];
		try
		{
			for(int i=0;i<temp.length;i++)
				temp[i]=f.read();
		}catch(IOException ioe){}
		int in[][]=new int [4][4];
		int out[][]=new int [4][4];
		result_text.setText("");
		for(int k=0;k<len/16;k++)
		{
			int m=0;
			for(int i=0;i<4;i++)
				for(int j=0;j<4;j++)
				{
					in[i][j]=temp[k*16+m];
					m++;
				}
			out = DecryptionProcess(in,key);
			try
			{
				for(int i=0;i<4;i++)
					for(int j=0;j<4;j++)
						tf.write((byte)out[i][j]);
			}catch(IOException ioe){}
		}
	}
	
	public int [][] DecryptionProcess(int [][]in,int [][]key)//解密
	{
		int w[][]=new int [44][4];
		w=AES.KeyExpansion(key,w);
		int temp[][]=AES.AddRoundKey(in,w,10);
		for(int i=9;i>0;i--)
			temp=AES.InvMixColumns(AES.AddRoundKey(AES.InvSubBytes(AES.InvShiftRows(temp)),w,i));
		int out[][]=AES.AddRoundKey(AES.InvSubBytes(AES.InvShiftRows(temp)),w,0);
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
				this.result_text.append(Integer.toHexString(out[i][j])+"\t");
			this.result_text.append("\n");
		}
		return out;
	}
	

⌨️ 快捷键说明

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