解释这个程序的作用import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame;…
- User
解释这个程序的作用import java.awt.Color;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;
class JF extends JFrame{ public JF(String s){ super(s); this.setLayout(null); this.setSize(700,400);;//设置窗口的位置以及大小 this.setBackground(Color.pink); //为窗体设置颜色 ////设置退出应用程序 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
class JP1 extends JPanel{ public JP1 () { this.setSize(700,700);//设置画布的位置以及大小 this.setBackground(Color.gray); init(); }
private void init() { // TODO Auto-generated method stub setLayout(null); //清空布局管理器 //布局一 JLabel title=new JLabel("SM4 + CBC算法加密解密演示");//设置标签 title.setBounds(200,0,400,15);//设置title的位置,大小 add(title);//添加进容器 //分组1 JLabel IV11=new JLabel("IV1"); IV11.setBounds(0, 30, 20, 20); JTextField IV1=new JTextField(10); IV1.setBounds(25, 30, 100, 20); add(IV11); add(IV1); JLabel xor1=new JLabel("xor"); xor1.setBounds(50, 50, 20, 20); add(xor1); JLabel string11=new JLabel("pt1"); string11.setBounds(0, 70, 20, 20); JTextField string1=new JTextField(10); string1.setBounds(25, 70, 100, 20); add(string11); add(string1); JButton button1 = new JButton("加密1"); button1.setBounds(30,110,80,20); add(button1); JLabel string55=new JLabel("ct1"); string55.setBounds(0, 140, 20, 20); JTextField string5=new JTextField(10); string5.setBounds(25, 140, 100, 20); add(string55); add(string5); JButton button5 = new JButton("解密1"); button5.setBounds(30,170,80,20); add(button5); JLabel xor5=new JLabel("xor"); xor5.setBounds(50, 200, 20, 20); add(xor5); JLabel IV55=new JLabel("IV1"); IV55.setBounds(50, 230, 20, 20); add(IV55); JLabel string99=new JLabel("pt1"); string99.setBounds(0, 260, 20, 20); JTextField string9=new JTextField(10); string9.setBounds(25, 260, 100, 20); add(string99); add(string9); //分组2 JLabel IV22=new JLabel("IV2"); IV22.setBounds(150, 30, 20, 20); JTextField IV2=new JTextField(10); IV2.setBounds(175, 30, 100, 20); add(IV22); add(IV2); JLabel xor2=new JLabel("xor"); xor2.setBounds(200, 50, 20, 20); add(xor2); JLabel string22=new JLabel("pt2"); string22.setBounds(150, 70, 20, 20); JTextField string2=new JTextField(10); string2.setBounds(175, 70, 100, 20); add(string22); add(string2); JButton button2 = new JButton("加密2"); button2.setBounds(180,110,80,20); add(button2); JLabel string66=new JLabel("ct2"); string66.setBounds(150, 140, 20, 20); JTextField string6=new JTextField(10); string6.setBounds(175, 140, 100, 20); add(string66); add(string6); JButton button6 = new JButton("解密2"); button6.setBounds(180,170,80,20); add(button6); JLabel xor6=new JLabel("xor"); xor6.setBounds(200, 200, 20, 20); add(xor6); JLabel IV66=new JLabel("IV1"); IV66.setBounds(200, 230, 20, 20); add(IV66); JLabel stringaa=new JLabel("pt2"); stringaa.setBounds(150, 260, 20, 20); JTextField stringa=new JTextField(10); stringa.setBounds(175, 260, 100, 20); add(stringaa); add(stringa); //分组3 JLabel IV33=new JLabel("IV3"); IV33.setBounds(300, 30, 20, 20); JTextField IV3=new JTextField(10); IV3.setBounds(325, 30, 100, 20); add(IV33); add(IV3); JLabel xor3=new JLabel("xor"); xor3.setBounds(350, 50, 20, 20); add(xor3); JLabel string33=new JLabel("pt3"); string33.setBounds(300, 70, 20, 20); JTextField string3=new JTextField(10); string3.setBounds(325, 70, 100, 20); add(string33); add(string3); JButton button3 = new JButton("加密3"); button3.setBounds(330,110,80,20); add(button3); JLabel string77=new JLabel("ct3"); string77.setBounds(300, 140, 20, 20); JTextField string7=new JTextField(10); string7.setBounds(325, 140, 100, 20); add(string77); add(string7); JButton button7 = new JButton("解密3"); button7.setBounds(330,170,80,20); add(button7); JLabel xor7=new JLabel("xor"); xor7.setBounds(350, 200, 20, 20); add(xor7); JLabel IV77=new JLabel("IV1"); IV77.setBounds(350, 230, 20, 20); add(IV77); JLabel stringbb=new JLabel("pt3"); stringbb.setBounds(300, 260, 20, 20); JTextField stringb=new JTextField(10); stringb.setBounds(325, 260, 100, 20); add(stringbb); add(stringb); //分组4 JLabel IV44=new JLabel("IV4"); IV44.setBounds(450, 30, 20, 20); JTextField IV4=new JTextField(10); IV4.setBounds(475, 30, 100, 20); add(IV44); add(IV4); JLabel xor4=new JLabel("xor"); xor4.setBounds(500, 50, 20, 20); add(xor4); JLabel string44=new JLabel("pt4"); string44.setBounds(450, 70, 20, 20); JTextField string4=new JTextField(10); string4.setBounds(475, 70, 100, 20); add(string44); add(string4); JButton button4 = new JButton("加密4"); button4.setBounds(480,110,80,20); add(button4); JLabel string88=new JLabel("ct4"); string88.setBounds(450, 140, 20, 20); JTextField string8=new JTextField(10); string8.setBounds(475, 140, 100, 20); add(string88); add(string8); JButton button8 = new JButton("解密4"); button8.setBounds(480,170,80,20); add(button8); JLabel xor8=new JLabel("xor"); xor8.setBounds(500, 200, 20, 20); add(xor8); JLabel IV88=new JLabel("IV1"); IV88.setBounds(500, 230, 20, 20); add(IV88); JLabel stringcc=new JLabel("pt4"); stringcc.setBounds(450, 260, 20, 20); JTextField stringc=new JTextField(10); stringc.setBounds(475, 260, 100, 20); add(stringcc); add(stringc); // JLabel key1=new JLabel("key"); key1.setBounds(575, 90, 20, 20); JTextField key=new JTextField(10); key.setBounds(600, 90, 100, 20); add(key); add(key1); //设置监听事件 button1.addActionListener(E1->{ byte[] KEY = key.getText().getBytes();//获取文本,转换成字节存储在数组KEY byte[] IV = IV1.getText().getBytes(); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = string1.getText().getBytes(); byte[] encript1 = ss1.encrypt(input); IV2.setText(hex(encript1)); string5.setText(hex(encript1)); }); button5.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = IV1.getText().getBytes(); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = hexStringToBytes(string5.getText());//将文本转为16进制字节 byte[] decript1 = ss1.decrypt(input); string9.setText(new String(decript1)); }); button2.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV2.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = string2.getText().getBytes(); byte[] encript1 = ss1.encrypt(input); IV3.setText(hex(encript1)); string6.setText(hex(encript1)); }); button6.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV2.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = hexStringToBytes(string6.getText()); byte[] decript1 = ss1.decrypt(input); stringa.setText(new String(decript1)); }); button3.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV3.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = string3.getText().getBytes(); byte[] encript1 = ss1.encrypt(input); IV4.setText(hex(encript1)); string7.setText(hex(encript1)); }); button7.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV3.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = hexStringToBytes(string7.getText()); byte[] decript1 = ss1.decrypt(input); stringb.setText(new String(decript1)); }); button4.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV4.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = string4.getText().getBytes(); byte[] encript1 = ss1.encrypt(input); string8.setText(hex(encript1)); }); button8.addActionListener(E1->{ byte[] KEY = key.getText().getBytes(); byte[] IV = hexStringToBytes(IV4.getText()); SM4CBC ss1 = new SM4CBC(KEY,IV); byte[] input = hexStringToBytes(string8.getText()); byte[] decript1 = ss1.decrypt(input); stringc.setText(new String(decript1)); }); } // public static String hex(byte[] bytes) { StringBuilder result = new StringBuilder(); for (byte aByte : bytes) { result.append(String.format("%02x", aByte)); } return result.toString(); } // public static byte[] hexStringToBytes(String hex) { byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16); } return bytes; }}
public class Main {
public static void main(String[] args) { // TODO Auto-generated method stub //界面设计 //创建窗口和画布 JF jf1=new JF("SM4算法演示"); JP1 jp=new JP1(); jf1.add(jp); jf1.setVisible(true); }}
- Assistant
这个程序是一个简单的GUI界面,用于演示SM4 + CBC算法的加密解密过程。程序包括一个窗口和一个画布,画布上有四个分组,每个分组都有一个加密、解密按钮和对应的文本框。用户可以在文本框中输入需要加密的数据和初始向量,点击加密按钮实现加密,点击解密按钮可以将加密后的数据解密出来。程序中采用了Java的Swing组件实现,其中JFrame和JPanel组件用于创建主窗口和画布。程序中还引用了Java的Color类实现窗口和画布的背景颜色设置,以及JButton、JLabel、JTextField等组件实现按钮、标签和文本框的创建和布局。通过该界面,用户可以轻松实现SM4 + CBC算法的加密解密操作,并了解该算法的基本原理和流程。