带界面的AES算法Java实现.docx_第1页
带界面的AES算法Java实现.docx_第2页
带界面的AES算法Java实现.docx_第3页
带界面的AES算法Java实现.docx_第4页
带界面的AES算法Java实现.docx_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

界面效果:窗口操作界面源代码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 javax.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 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 ButtonGroup 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(目标文件, SwingConstants.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 = 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();JPanel 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 northCenterPanel = 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(northPanel, 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);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 JPanel(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 = 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(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);methodRadioButtons0.addActionListener(this);methodRadioButtons1.addActionListener(this);/设置关闭按钮/create anonymous inner class for windows closing eventthis.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent event)/invoke exit in outer classSystem.exit(0);); pack(); /设置界面大小setSize(400,300);setLocation(0,0); setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource().equals(methodRadioButtons0)sourceLabel.setText(源文件);aimLabel.setText(目标文件);selectSourceBt.setEnabled(true);selectAimBt.setEnabled(true);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.getSource().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 selectFile()String rootine = ;File aFile = null; / 接收文件int result = 0; / 接收操作状态JFileChooser fileChooser = new JFileChooser();/ 文件选择框fileChooser.setApproveButtonText(确定);fileChooser.setDialogTitle(打开文件);fileChooser.setMultiSelectionEnabled(false); /不允许一次选择多个文件fileChooser.setCurrentDirectory(new 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=JFileChooser.CANCEL_OPTION)/选择取消,文件选择框正常关闭elseJOptionPane.showMessageDialog(fileChooser,文件选择框出现异常!);return rootine;public void encrypt()String source = sourceText.getText();String aim = aimText.getText();String key = keyText.getText();boolean isFile = methodRadioButtons0.getModel().isSelected();if(isFile)tryFile resour = new File(source);File arm= new File(aim);boolean flag = new Encrypt().fileEncrypt(resour, key, arm);if(flag)JOptionPane.showMessageDialog(this,加密完成! );elseJOptionPane.showMessageDialog(this,加密失败! );return;catch(Exception e)JOptionPane.showMessageDialog(this,加密失败! );return;elsetryString 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 = aimText.getText();String key = keyText.getText();boolean isFile = methodRadioButtons0.getModel().isSelected();if(isFile)tryFile resour = new File(source);File arm= new File(aim);boolean flag = new Decrypt().fileDecrypt(resour, key, arm);if(flag)JOptionPane.showMessageDialog(this,解密完成! );elseJOptionPane.showMessageDialog(this,解密失败! );return;catch(Exception e)JOptionPane.showMessageDialog(this,解密失败! );return;elsetryString result = new Decrypt().textDecrypt(source, key);aimText.setText(result);JOptionPane.showMessageDialog(this,解密完成! );return;catch(Exception e)JOptionPane.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 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,-15,113,-40,49,21,4,-57,35,-61,24,-106,5,-102,7,18,-128,-30,-21,39,-78,117,9,-125,44,26,27,110,90,-96,82,59,-42,-77,41,-29,47,-124,83,-47,0,-19,32,-4,-79,91,106,-53,-66,57,74,76,88,-49,-48,-17,-86,-5,67,77,51,-123,69,-7,2,127,80,60,-97,-88,81,-93,64,-113,-110,-99,56,-11,-68,-74,-38,33,16,-1,-13,-46,-51,12,19,-20,95,-105,68,23,-60,-89,126,61,100,93,25,115,96,-127,79,-36,34,42,-112,-120,70,-18,-72,20,-34,94,11,-37,-32,50,58,10,73,6,36,92,-62,-45,-84,98,-111,-107,-28,121,-25,-56,55,109,-115,-43,78,-87,108,86,-12,-22,101,122,-82,8,-70,120,37,46,28,-90,-76,-58,-24,-35,116,31,75,-67,-117,-118,112,62,-75,102,72,3,-10,14,97,53,87,-71,-122,-63,29,-98,-31,-8,-104,17,105,-39,-114,-108,-101,30,-121,-23,-50,85,40,-33,-116,-95,-119,13,-65,-26,66,104,65,-103,45,15,-80,84,-69,22,;private byte rsbox=82,9,106,-43,48,54,-91,56,-65,64,-93,-98,-127,-13,-41,-5,124,-29,57,-126,-101,47,-1,-121,52,-114,67,68,-60,-34,-23,-53,84,123,-108,50,-90,-62,35,61,-18,76,-107,11,66,-6,-61,78,8,46,-95,102,40,-39,36,-78,118,91,-94,73,109,-117,-47,37,114,-8,-10,100,-122,104,-104,22,-44,-92,92,-52,93,101,-74,-110,108,112,72,80,-3,-19,-71,-38,94,21,70,87,-89,-115,-99,-124,-112,-40,-85,0,-116,-68,-45,10,-9,-28,88,5,-72,-77,69,6,-48,44,30,-113,-54,63,15,2,-63,-81,-67,3,1,19,-118,107,58,-111,17,65,79,103,-36,-22,-105,-14,-49,-50,-16,-76,-26,115,-106,-84,116,34,-25,-83,53,-123,-30,-7,55,-24,28,117,-33,110,71,-15,26,113,29,41,-59,-119,111,-73,98,14,-86,24,-66,27,-4,86,62,75,-58,-46,121,32,-102,-37,-64,-2,120,-51,90,-12,31,-35,-88,51,-120,7,-57,49,-79,18,16,89,39,-128,-20,95,96,81,127,-87,25,-75,74,13,45,-27,122,-97,-109,-55,-100,-17,-96,-32,59,77,-82,42,-11,-80,-56,-21,-69,60,-125,83,-103,97,23,43,4,126,-70,119,-42,38,-31,105,20,99,85,33,12,125;private byte mut=2,3,1,1,1,2,3,1,1,1,2,3,3,1,1,2;private byte rmut=14,11,13,9,9,14,11,13,13,9,14,11,11,13,9,14;private byte by=1,2,4,8,16,32,64,-128,27,54,108,-40,-85,77,-102,;private int r=0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36;byte allkey=new byte1144;byte enbyte=new byte16;byte debyte=new byte16;/字节替代,public byte subbyte(byte sub)byte row,col;byte temp=new byte44;for(int i=0;i4;i+)for(int j=0;j4)&0xf);tempij=sboxrowcol;return temp;public byte subbyte(byte sub,int r)byte row,col;byte temp=new byte44;for(int i=0;i4;i+)for(int j=0;j4)&0xf);tempij=rsboxrowcol;return temp;/行移位public byte shift(byte sub)byte temp;temp=sub10;sub10=sub11;sub11=sub12;sub12=sub13;sub13=temp;temp=sub20;sub20=sub22;sub22=temp;temp=sub21;sub21=sub23;sub23=temp;temp=sub30;sub30=sub33;sub33=sub32;sub32=sub31;sub31=temp;return sub;public byte shift(byte sub,int mode)byte temp;temp=sub30;sub30=sub31;sub31=sub32;sub32=sub33;sub33=temp;temp=sub20;sub20=sub22;sub22=temp;temp=sub21;sub21=sub23;sub23=temp;temp=sub10;sub10=sub13;sub13=sub12;sub12=sub11;sub11=temp;return sub;/列混合public byte mix(byte sub)byte count=0;byte temp=new byte44;for(int i=0;i4;i+)for(int j=0;j4;j+)while(count4)tempij=(byte)(tempijmu(muticount,subcountj);count+;count=0;return temp;public byte mix(byte sub,int mode)byte count=0;byte temp=new byte44;for(int i=0;i4;i+)for(int j=0;j4;j+)while(count4)tempij=(byte)(tempijmu(rmuticount,subcountj);count+;count=0;return temp;/轮密钥加public byte add(byte sub,byte roundkey)for(int i=0;i4;i+)for(int j=0;j4;j+)subij=(byte)(subijroundkeyij);return sub;/列混合中所用函数public byte mu(byte b,byte c)byte ret=0 ,count1=0,count2=0;byte barray=new byte8;byte carray=new byte8;byte pro=new byte15;if(b=1|c=0)return c;if(c=1)return b;for(int i=0;i1);count1+;for(int i=0;i1);count2+;for(int i=0;icount1;i+)for(int j=0;j0&carrayj0)proi+j=(byte)(proi+j+1)%2);for(int m=0;m0)ret=(byte)(bym)(ret);return ret;/密钥扩展public byte key(byte okey)byte retarray=new byte1144;for(int i=0;i4;i+)for(int j=0;j4;j+)retarray0ji=okeyij;for(int i=1;i4)&0xf);temp00=(byte)(risboxrowcolokey00);col=(byte)(okey23&0xf);row=(byte)(okey234)&0xf);temp10=(byte)(sboxrowcolokey10);col=(byte)(okey33&0xf);row=(byte)(okey334)&0xf);temp20=(byte)(sboxrowcolokey20);col=(byte)(okey03&0xf);row=(byte)(okey034)&0xf);temp30=(byte)(sboxrowcolokey30);for(int i=1;i4;i+)temp0i=(byte)(temp0i-1okey0i);temp1i=(byte)(temp1i-1okey1i);temp2i=(byte)(temp2i-1okey2i);temp3i=(byte)(temp3i-1okey3i);return temp;/格式化输入密钥public byte kformat(String key)byte b=new byte44;int ret=new int16;int t,count=0,i=0;for(;i+count15&(ikey.length();i+)t=key.charAt(i);if(t8;reti+count=t;count+;if(i+count=15)if(key.charAt(i)128) ret15=key.charAt(i);for(int j=0;j4;j+)for(int k=0;k1)/读取一个密文组到debyte16bufin.read(debyte);de();/写入一个解密组bufout.write(enbyte);by-;bufin.read(debyte);de();int fill=bufin.read();/判断是否有填充if(fill16)bufout.write(enbyte);elsebytelast=new bytefill;for(byte i=0;ilast.length

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论