1、Java实现AES算法+界面Java源程序:import java.awt.BorderLayout;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import j
2、avax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JTextField;import javax.swing.SwingConstants;public class
3、AESFrame extends JFrame implements ActionListener private JLabel sourceLabel, aimLabel, keyLabel; private JTextField sourceText, aimText, keyText; private JButton encryptBt, decryptBt, clearBt, exitBt, selectSourceBt, selectAimBt; private JRadioButton methodRadioButtons=new JRadioButton2; private Bu
4、ttonGroup methodGroup=new ButtonGroup(); public static void main(String args) new AESFrame(AES加密解密工具 By:*); public AESFrame(String title) super(title); /初始化组件 keyLabel = new JLabel(密钥, SwingConstants.RIGHT); sourceLabel = new JLabel(源文件, SwingConstants.RIGHT); aimLabel = new JLabel(目标文件, SwingConsta
5、nts.RIGHT); keyText = new JTextField(20); sourceText = new JTextField(20); aimText = new JTextField(20); encryptBt = new JButton(加密); decryptBt = new JButton(解密); clearBt = new JButton(清空); exitBt = new JButton(退出); selectSourceBt = new JButton(浏览); selectAimBt = new JButton(浏览); methodRadioButtons0
6、 = new JRadioButton(文件); methodRadioButtons1 = new JRadioButton(字符串); methodGroup.add(methodRadioButtons0); methodGroup.add(methodRadioButtons1); methodRadioButtons0.setSelected(true); /划分面板 Container c = this.getContentPane(); c.setLayout(new BorderLayout(); JPanel centerPanel = new JPanel(); JPane
7、l northPanel = new JPanel(); JPanel southPanel = new JPanel(); JPanel eastPanel = new JPanel(); JPanel westPanel = new JPanel(); c.add(eastPanel, BorderLayout.EAST); c.add(westPanel, BorderLayout.WEST); northPanel.setLayout(new GridLayout(1, 3); JPanel northLeftPanel = new JPanel(); JPanel northCent
8、erPanel = new JPanel(); JPanel northRightPanel = new JPanel(); northCenterPanel.setLayout(new FlowLayout(); northCenterPanel.add(methodRadioButtons0); northCenterPanel.add(methodRadioButtons1); northPanel.add(northLeftPanel); northPanel.add(northCenterPanel); northPanel.add(northRightPanel); c.add(n
9、orthPanel, BorderLayout.NORTH); southPanel.setLayout(new GridLayout(1, 2); JPanel southLeftPanel = new JPanel(new FlowLayout(); JPanel southRightPanel = new JPanel(new FlowLayout(); southLeftPanel.add(encryptBt); southLeftPanel.add(decryptBt); southRightPanel.add(clearBt); southRightPanel.add(exitBt
10、); southPanel.add(southLeftPanel); southPanel.add(southRightPanel); c.add(southPanel, BorderLayout.SOUTH); centerPanel.setLayout(new BorderLayout(); JPanel centerEastPanel = new JPanel(new GridLayout(3, 1); JPanel centerWestPanel = new JPanel(new GridLayout(3, 1); JPanel centerCenterPanel = new JPan
11、el(new GridLayout(3, 1); JPanel centerSouthPanel = new JPanel(); JPanel centerNorthPanel = new JPanel(); JPanel panel1 = new JPanel(new FlowLayout(); JPanel panel2 = new JPanel(new FlowLayout(); JPanel panel3 = new JPanel(new FlowLayout(); JPanel panel4 = new JPanel(new FlowLayout(); JPanel panel5 =
12、 new JPanel(new FlowLayout(); JPanel panel6 = new JPanel(new FlowLayout(); JPanel panel7 = new JPanel(new FlowLayout(); JPanel panel8 = new JPanel(new FlowLayout(); JPanel panel9 = new JPanel(new FlowLayout(); panel1.add(sourceLabel); panel2.add(sourceText); panel3.add(selectSourceBt); panel4.add(ai
13、mLabel); panel5.add(aimText); panel6.add(selectAimBt); panel7.add(keyLabel); panel8.add(keyText); centerWestPanel.add(panel1); centerWestPanel.add(panel4); centerWestPanel.add(panel7); centerCenterPanel.add(panel2); centerCenterPanel.add(panel5); centerCenterPanel.add(panel8); centerEastPanel.add(pa
14、nel3); centerEastPanel.add(panel6); centerEastPanel.add(panel9); centerPanel.add(centerEastPanel, BorderLayout.EAST); centerPanel.add(centerWestPanel, BorderLayout.WEST); centerPanel.add(centerCenterPanel, BorderLayout.CENTER); centerPanel.add(centerSouthPanel, BorderLayout.SOUTH); centerPanel.add(c
15、enterNorthPanel, BorderLayout.NORTH); c.add(centerPanel, BorderLayout.CENTER); /加入按钮监听 encryptBt.addActionListener(this); decryptBt.addActionListener(this); clearBt.addActionListener(this); exitBt.addActionListener(this); selectSourceBt.addActionListener(this); selectAimBt.addActionListener(this); m
16、ethodRadioButtons0.addActionListener(this); methodRadioButtons1.addActionListener(this); /设置关闭按钮 /create anonymous inner class for windows closing event this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent event) /invoke exit in outer class System.exit(0); ); pack(); /设置界
17、面大小 setSize(400,300); setLocation(0,0); setVisible(true); Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub if(e.getSource().equals(methodRadioButtons0) sourceLabel.setText(源文件); aimLabel.setText(目标文件); selectSourceBt.setEnabled(true); selectAimBt.setEnabled(true)
18、; clear(); if(e.getSource().equals(methodRadioButtons1) sourceLabel.setText(源字符串); aimLabel.setText(目标字符串); selectSourceBt.setEnabled(false); selectAimBt.setEnabled(false); clear(); if(e.getSource().equals(selectSourceBt) String sourceFile = selectFile(); sourceText.setText(sourceFile); if(e.getSour
19、ce().equals(selectAimBt) String aimFile = selectFile(); aimText.setText(aimFile); if(e.getSource().equals(encryptBt) encrypt(); if(e.getSource().equals(decryptBt) decrypt(); if(e.getSource().equals(exitBt) System.exit(0); if(e.getSource().equals(clearBt) clear(); keyText.setText(); private String se
20、lectFile() String rootine = ; File aFile = null; / 接收文件 int result = 0; / 接收操作状态 JFileChooser fileChooser = new JFileChooser(); / 文件选择框 fileChooser.setApproveButtonText(确定); fileChooser.setDialogTitle(打开文件); fileChooser.setMultiSelectionEnabled(false); /不允许一次选择多个文件 fileChooser.setCurrentDirectory(ne
21、w File(.AES); /设置默认文件夹路径 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); /只允许选择文件,不允许选择文件夹 /设置文件过滤器 result = fileChooser.showOpenDialog(this); if(result = JFileChooser.APPROVE_OPTION) / 选择的是确定按钮 aFile = fileChooser.getSelectedFile(); / 得到选择的文件 rootine = aFile.getPath(); else if(result=JFi
22、leChooser.CANCEL_OPTION)/选择取消,文件选择框正常关闭 else JOptionPane.showMessageDialog(fileChooser,文件选择框出现异常!); return rootine; public void encrypt() String source = sourceText.getText(); String aim = aimText.getText(); String key = keyText.getText(); boolean isFile = methodRadioButtons0.getModel().isSelected()
23、; if(isFile) try File resour = new File(source); File arm= new File(aim); boolean flag = new Encrypt().fileEncrypt(resour, key, arm); if(flag) JOptionPane.showMessageDialog(this,加密完成! ); else JOptionPane.showMessageDialog(this,加密失败! ); return; catch(Exception e) JOptionPane.showMessageDialog(this,加密
24、失败! ); return; else try String result = new Encrypt().textEncrypt(source, key); aimText.setText(result); JOptionPane.showMessageDialog(this,加密完成! ); return; catch(Exception e) JOptionPane.showMessageDialog(this,加密失败! ); return; public void decrypt() String source = sourceText.getText(); String aim =
25、 aimText.getText(); String key = keyText.getText(); boolean isFile = methodRadioButtons0.getModel().isSelected(); if(isFile) try File resour = new File(source); File arm= new File(aim); boolean flag = new Decrypt().fileDecrypt(resour, key, arm); if(flag) JOptionPane.showMessageDialog(this,解密完成! ); e
26、lse JOptionPane.showMessageDialog(this,解密失败! ); return; catch(Exception e) JOptionPane.showMessageDialog(this,解密失败! ); return; else try String result = new Decrypt().textDecrypt(source, key); aimText.setText(result); JOptionPane.showMessageDialog(this,解密完成! ); return; catch(Exception e) JOptionPane.
27、showMessageDialog(this,解密失败! ); return; private void clear() sourceText.setText(); aimText.setText(); AES算法实现源代码import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import
28、 java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.InputStreamReader;import java.io.IOException;/AES算法几个变换函数class Aes private byte sbox= 99,124,119,123,-14,107,111,-59,48,1,103,43,-2,-41,-85,118, -54,-126,-55,125,-6,89,71,-16,-83,-44,-94,-81,-100,-92,114,-64, -73,-3,-109,38,54,63,-9,-52,52,-91,-27,-1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1