带界面的AES算法Java实现
更新时间:2023-11-08 23:24:01 阅读量: 教育文库 文档下载
- 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 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 JRadioButton[2]; private ButtonGroup methodGroup=new ButtonGroup();
public static void main(String[] args) { new AESFrame(\加密解密工具 By:***\ } public AESFrame(String title) { super(title); //初始化组件 keyLabel = new JLabel(\密钥\ sourceLabel = new JLabel(\源文件\
SwingConstants.RIGHT); aimLabel = new JLabel(\目标文件\ 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(\浏览\ methodRadioButtons[0] = new JRadioButton(\文件\ methodRadioButtons[1] = new JRadioButton(\字符串\ methodGroup.add(methodRadioButtons[0]); methodGroup.add(methodRadioButtons[1]); methodRadioButtons[0].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);
1)); 1)); 1));
northPanel.setLayout(new GridLayout(1, 3)); JPanel northLeftPanel = new JPanel(); JPanel northCenterPanel = new JPanel(); JPanel northRightPanel = new JPanel();
northCenterPanel.setLayout(new FlowLayout()); northCenterPanel.add(methodRadioButtons[0]); northCenterPanel.add(methodRadioButtons[1]); 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,
JPanel centerWestPanel = new JPanel(new GridLayout(3,
JPanel centerCenterPanel = new JPanel(new GridLayout(3,
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);
methodRadioButtons[0].addActionListener(this); methodRadioButtons[1].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(); //设置界面大小 setSize(400,300); setLocation(0,0); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub 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)) { 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(\
//设置默认文件夹路径 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){}//选择取消
,文件选择框正常关闭 else { JOptionPane.showMessageDialog(fileChooser,\文件
选择框出现异常!\ } return rootine; } public void encrypt() { String source = sourceText.getText(); String aim = aimText.getText(); String key = keyText.getText(); boolean isFile = methodRadioButtons[0].getModel
().isSelected(); 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,\加密
失败! \ 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 = aimText.getText(); String key = keyText.getText(); boolean isFile = methodRadioButtons[0].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,\解密完成! \ }else{ 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.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 byte[11][4][4]; byte[] enbyte=new byte[16]; byte[] debyte=new byte[16]; //字节替代,
public byte[][] subbyte(byte[][] sub){ byte row,col; byte[][] temp=new byte[4][4]; for(int i=0;i<4;i++) for(int j=0;j<4;j++){ col=(byte)(sub[i][j]&0xf); row=(byte)((sub[i][j]>>4)&0xf); temp[i][j]=sbox[row][col]; } return temp; }
public byte[][] subbyte(byte[][] sub,int r){ byte row,col; byte[][] temp=new byte[4][4]; for(int i=0;i<4;i++) for(int j=0;j<4;j++){ col=(byte)(sub[i][j]&0xf); row=(byte)((sub[i][j]>>4)&0xf); temp[i][j]=rsbox[row][col]; } return temp; }
//行移位
public byte[][] shift(byte[][] sub){ byte temp; temp=sub[1][0]; sub[1][0]=sub[1][1];sub[1][1]=sub[1][2];sub[1][2]=sub[1][3];sub[1][3]=temp; temp=sub[2][0]; sub[2][0]=sub[2][2];sub[2][2]=temp; temp=sub[2][1]; sub[2][1]=sub[2][3];sub[2][3]=temp; temp=sub[3][0]; sub[3][0]=sub[3][3];sub[3][3]=sub[3][2];sub[3][2]=sub[3][1];sub[3][1]=temp; return sub; }
public byte[][] shift(byte[][] sub,int mode){ byte temp; temp=sub[3][0];
sub[3][0]=sub[3][1];sub[3][1]=sub[3][2];sub[3][2]=sub[3][3];sub[3][3]=temp; temp=sub[2][0]; sub[2][0]=sub[2][2];sub[2][2]=temp; temp=sub[2][1]; sub[2][1]=sub[2][3];sub[2][3]=temp; temp=sub[1][0]; sub[1][0]=sub[1][3];sub[1][3]=sub[1][2];sub[1][2]=sub[1][1];sub[1][1]=temp; return sub; }
//列混合
public byte[][] mix(byte[][] sub){ byte count=0; byte[][] temp=new byte[4][4]; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ while(count<4){ temp[i][j]=(byte)(temp[i][j]^mu(mut[i][count],sub[count][j])); count++; } count=0; } } return temp; }
public byte[][] mix(byte[][] sub,int mode){ byte count=0; byte[][] temp=new byte[4][4]; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ while(count<4){ temp[i][j]=(byte)(temp[i][j]^mu(rmut[i][count],sub[count][j])); count++; } count=0; } } return temp; }
//轮密钥加
public byte[][] add(byte sub[][],byte[][] roundkey){ for(int i=0;i<4;i++) for(int j=0;j<4;j++){
sub[i][j]=(byte)(sub[i][j]^roundkey[i][j]); } return sub; }
//列混合中所用函数
public byte mu(byte b,byte c){ byte ret=0 ,count1=0,count2=0; byte[] barray=new byte[8]; byte[] carray=new byte[8]; byte[] pro=new byte[15]; if(b==1|c==0)return c; if(c==1)return b; for(int i=0;i<8&&b!=0;i++){ barray[i]=(byte)(b&1); b=(byte)(b>>1); count1++; } for(int i=0;i<8&&c!=0;i++){ carray[i]=(byte)(c&1); c=(byte)(c>>1); count2++; } for(int i=0;i
//密钥扩展
public byte[][][] key(byte[][] okey){ byte[][][] retarray=new byte[11][4][4]; for(int i=0;i<4;i++) for(int j=0;j<4;j++){ retarray[0][j][i]=okey[i][j]; } for(int i=1;i<11;i++){ retarray[i]=tkey(retarray[i-1],r[i]); }
return retarray; }
//密钥扩展中所用函数
public byte[][] tkey(byte[][] okey,int ri){ byte[][] temp=new byte[4][4]; byte col,row; col=(byte)(okey[1][3]&0xf); row=(byte)((okey[1][3]>>4)&0xf); temp[0][0]=(byte)(ri^sbox[row][col]^okey[0][0]); col=(byte)(okey[2][3]&0xf); row=(byte)((okey[2][3]>>4)&0xf); temp[1][0]=(byte)(sbox[row][col]^okey[1][0]); col=(byte)(okey[3][3]&0xf); row=(byte)((okey[3][3]>>4)&0xf); temp[2][0]=(byte)(sbox[row][col]^okey[2][0]); col=(byte)(okey[0][3]&0xf); row=(byte)((okey[0][3]>>4)&0xf); temp[3][0]=(byte)(sbox[row][col]^okey[3][0]); for(int i=1;i<4;i++){ temp[0][i]=(byte)(temp[0][i-1]^okey[0][i]); temp[1][i]=(byte)(temp[1][i-1]^okey[1][i]); temp[2][i]=(byte)(temp[2][i-1]^okey[2][i]); temp[3][i]=(byte)(temp[3][i-1]^okey[3][i]); } return temp; }
//格式化输入密钥
public byte[][] kformat(String key){ byte[][] b=new byte[4][4]; int[] ret=new int[16]; int t,count=0,i=0; for(;i+count<15&(i
b[j][k]=(byte)ret[j*4+k]; return b; } }
class Decrypt extends Aes{ //文件解密的实现 public boolean fileDecrypt(File ori,String key,File cur){ //读取长度 long flag=ori.length(); //读取组数 long by=flag/16; //判断是否可解密(加密完成后会在末尾添加一个字节用于描述前一组明文的填充数,所以分组数mod16必为1) if(flag==1){ allkey=key(kformat(key)); try{ //包装流 BufferedOutputStream bufout=new BufferedOutputStream(new FileOutputStream(cur,true)); BufferedInputStream bufin=new BufferedInputStream(new FileInputStream(ori)); while(by>1){ //读取一个密文组到debyte[16] bufin.read(debyte); de(); //写入一个解密组 bufout.write(enbyte); by--; } bufin.read(debyte); de(); int fill=bufin.read(); //判断是否有填充 if(fill>16) bufout.write(enbyte); else{ byte[]last=new byte[fill]; for(byte i=0;i bufout.close(); return true; }catch(Exception e){return false;} }else return false; } //文本解密的实现 public String textDecrypt(String content,String key){ int flag=0;char[] bufchar=new char[1];String ret=\ try{ File temp_Dd=new File(\ File temp_D=new File(\ temp_Dd.createNewFile(); temp_D.createNewFile(); BufferedOutputStream bufw=new BufferedOutputStream(new FileOutputStream(temp_Dd)); int index=content.length()/2; //文本十六进制转换为文件流 byte[] b=new byte[index]; for(int i=0;i temp=add(temp,allkey[10]); for(int i=1;i<10;i++) temp=mix(add(subbyte(shift(temp,-1),-1),allkey[10-i]),-1); temp=add(subbyte(shift(temp, -1), -1), allkey[0]); for(int i=0;i<4;i++) for(int j=0;j<4;j++) enbyte[i*4+j]=temp[j][i]; } } class Encrypt extends Aes { //文件加密 public boolean fileEncrypt(File ori,String key,File cur){ int flag=16; allkey=key(this.kformat(key)); try{ BufferedOutputStream bufout=new BufferedOutputStream(new FileOutputStream(cur,true)); BufferedInputStream bufin=new BufferedInputStream(new FileInputStream(ori)); flag=bufin.read(enbyte); //判断是否明文分组足够 while(flag>15){ en(); bufout.write(debyte); flag=bufin.read(enbyte); } //判断明文是否剩余 if(flag>0){ en(); bufout.write(debyte); bufout.write(flag); } else bufout.write(17); bufin.close(); bufout.close(); return true; }catch(Exception e){cur.delete();return false;} } //文本加密 public String textEncrypt(String content,String key){ int flag=0;String ret=\ try{ File temp_Ed=new File(\ File temp_E=new File (\ temp_Ed.createNewFile(); temp_E.createNewFile(); //文本流转换文件流 BufferedWriter bufw=new BufferedWriter(new FileWriter(temp_Ed)); bufw.write(content); bufw.close(); //调用文件加密算法 if(fileEncrypt(temp_Ed, key,temp_E)){ BufferedInputStream bufr=new BufferedInputStream(new FileInputStream(temp_E)); flag=bufr.read(); //文件流转换十六进制流 while(flag!=-1){ if(flag<16) ret+=\ ret+=Integer.toHexString(flag); flag=bufr.read();} bufr.close(); } temp_Ed.delete(); temp_E.delete(); return ret; }catch(Exception e){return null;} } //一个明文组加密的实现 public void en(){ byte[][] temp=new byte[4][4]; for(int i=0;i<4;i++) for(int j=0;j<4;j++) temp[i][j]=enbyte[j*4+i]; temp=add(temp, allkey[0]); for(int i=1;i<10;i++) temp=this.add(mix(shift(subbyte(temp))),allkey[i]); temp=this.add(this.shift(this.subbyte(temp)), allkey[10]); for(int i=0;i<4;i++) for(int j=0;j<4;j++) debyte[i*4+j]=temp[j][i]; } }
正在阅读:
带界面的AES算法Java实现11-08
改革材料常用对偶过渡句02-23
民间俗语大全06-28
开展世界睡眠日主题宣传活动新闻稿02-23
公文写作经典素材 公文写作常用过渡句02-23
江苏省委党校社会心理试题及答案12-19
陕西省西安市高新第一中学高一历史导与练第16课《抗日战争》导与练(新人教版必修1)01-03
公文写作经典素材_过渡句02-23
海港通用码头建设标准05-28
湖南二级注册建筑师挂靠是多少钱一年02-11
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 算法
- 面的
- 实现
- Java
- AES