带界面的AES算法Java实现.docx
- 文档编号:11455492
- 上传时间:2023-03-01
- 格式:DOCX
- 页数:31
- 大小:46.47KB
带界面的AES算法Java实现.docx
《带界面的AES算法Java实现.docx》由会员分享,可在线阅读,更多相关《带界面的AES算法Java实现.docx(31页珍藏版)》请在冰豆网上搜索。
带界面的AES算法Java实现
界面效果:
窗口操作界面源代码
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.File;
importjavax.swing.ButtonGroup;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JRadioButton;
importjavax.swing.JTextField;
importjavax.swing.SwingConstants;
publicclassAESFrameextendsJFrameimplementsActionListener
{
privateJLabelsourceLabel,aimLabel,keyLabel;
privateJTextFieldsourceText,aimText,keyText;
privateJButtonencryptBt,decryptBt,clearBt,exitBt,
selectSourceBt,selectAimBt;
privateJRadioButton[]methodRadioButtons=newJRadioButton[2];
privateButtonGroupmethodGroup=newButtonGroup();
publicstaticvoidmain(String[]args)
{
newAESFrame("AES加密解密工具By:
***");
}
publicAESFrame(Stringtitle)
{
super(title);
//初始化组件
keyLabel=newJLabel("密钥",SwingConstants.RIGHT);
sourceLabel=newJLabel("源文件",
SwingConstants.RIGHT);
aimLabel=newJLabel("目标文件",SwingConstants.RIGHT);
keyText=newJTextField(20);
sourceText=newJTextField(20);
aimText=newJTextField(20);
encryptBt=newJButton("加密");
decryptBt=newJButton("解密");
clearBt=newJButton("清空");
exitBt=newJButton("退出");
selectSourceBt=newJButton("浏览");
selectAimBt=newJButton("浏览");
methodRadioButtons[0]=newJRadioButton("文件");
methodRadioButtons[1]=newJRadioButton("字符串");
methodGroup.add(methodRadioButtons[0]);
methodGroup.add(methodRadioButtons[1]);
methodRadioButtons[0].setSelected(true);
//划分面板
Containerc=this.getContentPane();
c.setLayout(newBorderLayout());
JPanelcenterPanel=newJPanel();
JPanelnorthPanel=newJPanel();
JPanelsouthPanel=newJPanel();
JPaneleastPanel=newJPanel();
JPanelwestPanel=newJPanel();
c.add(eastPanel,BorderLayout.EAST);
c.add(westPanel,BorderLayout.WEST);
northPanel.setLayout(newGridLayout(1,3));
JPanelnorthLeftPanel=newJPanel();
JPanelnorthCenterPanel=newJPanel();
JPanelnorthRightPanel=newJPanel();
northCenterPanel.setLayout(newFlowLayout());
northCenterPanel.add(methodRadioButtons[0]);
northCenterPanel.add(methodRadioButtons[1]);
northPanel.add(northLeftPanel);
northPanel.add(northCenterPanel);
northPanel.add(northRightPanel);
c.add(northPanel,BorderLayout.NORTH);
southPanel.setLayout(newGridLayout(1,2));
JPanelsouthLeftPanel=newJPanel(newFlowLayout());
JPanelsouthRightPanel=newJPanel(newFlowLayout());
southLeftPanel.add(encryptBt);
southLeftPanel.add(decryptBt);
southRightPanel.add(clearBt);
southRightPanel.add(exitBt);
southPanel.add(southLeftPanel);
southPanel.add(southRightPanel);
c.add(southPanel,BorderLayout.SOUTH);
centerPanel.setLayout(newBorderLayout());
JPanelcenterEastPanel=newJPanel(newGridLayout(3,
1));
JPanelcenterWestPanel=newJPanel(newGridLayout(3,
1));
JPanelcenterCenterPanel=newJPanel(newGridLayout(3,
1));
JPanelcenterSouthPanel=newJPanel();
JPanelcenterNorthPanel=newJPanel();
JPanelpanel1=newJPanel(newFlowLayout());
JPanelpanel2=newJPanel(newFlowLayout());
JPanelpanel3=newJPanel(newFlowLayout());
JPanelpanel4=newJPanel(newFlowLayout());
JPanelpanel5=newJPanel(newFlowLayout());
JPanelpanel6=newJPanel(newFlowLayout());
JPanelpanel7=newJPanel(newFlowLayout());
JPanelpanel8=newJPanel(newFlowLayout());
JPanelpanel9=newJPanel(newFlowLayout());
panel1.add(sourceLabel);
panel2.add(sourceText);
panel3.add(selectSourceBt);
panel4.add(aimLabel);
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(panel3);
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(centerNorthPanel,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);
methodRadioButtons[0].addActionListener(this);
methodRadioButtons[1].addActionListener(this);
//设置关闭按钮
//createanonymousinnerclassforwindowsclosingevent
this.addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEventevent)
{
//invokeexitinouterclass
System.exit(0);
}
});
pack();
//设置界面大小
setSize(400,300);
setLocation(0,0);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEvente)
{
//TODOAuto-generatedmethodstub
if(e.getSource().equals(methodRadioButtons[0]))
{
sourceLabel.setText("源文件");
aimLabel.setText("目标文件");
selectSourceBt.setEnabled(true);
selectAimBt.setEnabled(true);
clear();
}
if(e.getSource().equals(methodRadioButtons[1]))
{
sourceLabel.setText("源字符串");
aimLabel.setText("目标字符串");
selectSourceBt.setEnabled(false);
selectAimBt.setEnabled(false);
clear();
}
if(e.getSource().equals(selectSourceBt))
{
StringsourceFile=selectFile();
sourceText.setText(sourceFile);
}
if(e.getSource().equals(selectAimBt))
{
StringaimFile=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("");
}
}
privateStringselectFile()
{
Stringrootine="";
FileaFile=null;//接收文件
intresult=0;//接收操作状态
JFileChooserfileChooser=newJFileChooser();//文件
选择框
fileChooser.setApproveButtonText("确定");
fileChooser.setDialogTitle("打开文件");
fileChooser.setMultiSelectionEnabled(false);//不允
许一次选择多个文件
fileChooser.setCurrentDirectory(newFile("..\\AES"));
//设置默认文件夹路径
fileChooser.setFileSelectionMode
(JFileChooser.FILES_ONLY);//只允许选择文件,不允许选择文件夹
//设置文件过滤器
result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.APPROVE_OPTION)//选择
的是确定按钮
{
aFile=fileChooser.getSelectedFile();//得到
选择的文件
rootine=aFile.getPath();
}
elseif(result==JFileChooser.CANCEL_OPTION){}//选择取消
,文件选择框正常关闭
else
{
JOptionPane.showMessageDialog(fileChooser,"文件
选择框出现异常!
");
}
returnrootine;
}
publicvoidencrypt()
{
Stringsource=sourceText.getText();
Stringaim=aimText.getText();
Stringkey=keyText.getText();
booleanisFile=methodRadioButtons[0].getModel
().isSelected();
if(isFile)
{
try
{
Fileresour=newFile(source);
Filearm=newFile(aim);
booleanflag=newEncrypt
().fileEncrypt(resour,key,arm);
if(flag){
JOptionPane.showMessageDialog
(this,"加密完成!
");
}else{
JOptionPane.showMessageDialog
(this,"加密失败!
");
}
return;
}
catch(Exceptione)
{
JOptionPane.showMessageDialog(this,"加密
失败!
");
return;
}
}
else
{
try
{
Stringresult=newEncrypt
().textEncrypt(source,key);
aimText.setText(result);
JOptionPane.showMessageDialog(this,"加密
完成!
");
return;
}
catch(Exceptione)
{
JOptionPane.showMessageDialog(this,"加密
失败!
");
return;
}
}
}
publicvoiddecrypt()
{
Stringsource=sourceText.getText();
Stringaim=aimText.getText();
Stringkey=keyText.getText();
booleanisFile=methodRadioButtons[0].getModel
().isSelected();
if(isFile)
{
try
{
Fileresour=newFile(source);
Filearm=newFile(aim);
booleanflag=newDecrypt
().fileDecrypt(resour,key,arm);
if(flag){
JOptionPane.showMessageDialog
(this,"解密完成!
");
}else{
JOptionPane.showMessageDialog
(this,"解密失败!
");
}
return;
}
catch(Exceptione)
{
JOptionPane.showMessageDialog(this,"解密
失败!
");
return;
}
}
else
{
try
{
Stringresult=newDecrypt
().textDecrypt(source,key);
aimText.setText(result);
JOptionPane.showMessageDialog(this,"解密
完成!
");
return;
}
catch(Exceptione)
{
JOptionPane.showMessageDialog(this,"解密
失败!
");
return;
}
}
}
privatevoidclear()
{
sourceText.setText("");
aimText.setText("");
}
}
AES算法实现源代码
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.InputStreamReader;
importjava.io.IOException;
//AES算法几个变换函数
classAes{
privatebyte[][]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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 界面 AES 算法 Java 实现
![提示](https://static.bdocx.com/images/bang_tan.gif)