import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
// 图形界面结果
new PrjView();
/*
* // 命令行界面结果 int choice; String algorithm = "DES/ECB/noPadding"; String
* message = "123"; String keyStr = "123"; String iv = "123"; long
* runTime = 0;
*
* while (true) { choice = choose();
*
* switch (choice) { case 1: System.out.println("DES加密模式:"); runTime =
* System.currentTimeMillis(); DesEn de = new DesEn(algorithm, keyStr,
* message.getBytes()); byte[] msgDes = de.CipherMsg();
*
* System.out.println("加密后的密文为:" + new String(msgDes));
* System.out.println("解密密文为:" + new String(de.EncipherMsg(msgDes,
* keyStr)).trim()); runTime = System.currentTimeMillis() - runTime;
* System.out.println("程序执行了" + runTime + "毫秒。"); System.out.println();
* break; case 2: System.out.println("CBC加密模式:"); runTime =
* System.currentTimeMillis(); CbcEn ce = new CbcEn(algorithm, keyStr,
* iv, message.getBytes()); byte[] msgCbc = ce.CipherMsg();
* System.out.println("加密后的密文为:" + new String(msgCbc)); System.out
* .println("解密密文为:" + new String(ce.EncipherMsg(msgCbc, iv, keyStr))
* .trim()); runTime = System.currentTimeMillis() - runTime;
* System.out.println("程序执行了" + runTime + "毫秒。"); System.out.println();
* break; case 3: System.out.println("CFB加密模式:"); runTime =
* System.currentTimeMillis(); CfbEn cfe = new CfbEn(algorithm, keyStr,
* iv, message.getBytes()); byte[] msgCfb = cfe.CipherMsg();
* System.out.println("加密后的密文为:" + new String(msgCfb));
* System.out.println("解密密文为:" + new String(cfe.EncipherMsg(msgCfb, iv,
* keyStr)) .trim()); runTime = System.currentTimeMillis() - runTime;
* System.out.println("程序执行了" + runTime + "毫秒。"); System.out.println();
* break; case 4: System.out.println("OFB加密模式:"); runTime =
* System.currentTimeMillis(); OfbEn ofe = new OfbEn(algorithm, keyStr,
* iv, message.getBytes()); byte[] msgOfb = ofe.CipherMsg();
* System.out.println("加密后的密文为:" + new String(msgOfb));
* System.out.println("解密密文为:" + new String(ofe.EncipherMsg(msgOfb, iv,
* keyStr)) .trim()); runTime = System.currentTimeMillis() - runTime;
* System.out.println("程序执行了" + runTime + "毫秒。"); System.out.println();
* break; default: System.out.println("未选择加密模式,程序退出。"); System.exit(0);
* } }
*/
}
/*
* public static int choose() { int choice = 0;
* System.out.println("请输入选项符号选择加密模式。"); System.out.println("1:DES加密");
* System.out.println("2:CBC加密"); System.out.println("3:CFB加密");
* System.out.println("4:OFB加密"); System.out.println("其他:退出程序");
* BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
* String line; System.out.flush(); try { line = br.readLine(); choice =
* Integer.parseInt(line); } catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); } catch (NumberFormatException e) { //
* TODO: handle exception System.out.println("输入非数字字符!程序退出。");
* System.exit(0); } return choice; }
*/
}
@SuppressWarnings("serial")
class PrjView extends JFrame implements ActionListener {
// 定义准备让用户输入加密字符串、秘钥以及种子的控件
private JLabel lblMsg;
private JLabel lblKeyStr;
private JLabel lblIv;
private JTextField msg;
private JTextField keyStr;
private JTextField seed;
// 定义显示密文以及解密结果的控件
private JLabel lblCipher;
private JTextField cipherText;
private JLabel lblEnCipher;
private JTextField enCipherText;
// 选择加密方式的按钮
private JButton btnDes;
private JButton btnCbc;
private JButton btnCfb;
private JButton btnOfb;
// 显示加密解密运行时间的控件
private JLabel lblRunTime;
private JTextField runTime;
private JLabel lblUnit;
public PrjView() {
// 布局方式
this.setLayout(new FlowLayout());
// 输入欲加密字符串
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
lblMsg = new JLabel("欲加密字符串:");
msg = new JTextField(20);
p1.add(lblMsg, BorderLayout.WEST);
p1.add(msg, BorderLayout.EAST);
// 输入加密秘钥,秘钥只有前8字节有效,不够8字节末尾补零
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
lblKeyStr = new JLabel("加密密钥:");
keyStr = new JTextField(20);
p2.add(lblKeyStr, BorderLayout.WEST);
p2.add(keyStr, BorderLayout.EAST);
// 输入初始加密种子,只有前8字节有效,不够8字节末尾补零
JPanel ps = new JPanel();
ps.setLayout(new BorderLayout());
lblIv = new JLabel("初始种子:");
seed = new JTextField(20);
ps.add(lblIv, BorderLayout.WEST);
ps.add(seed, BorderLayout.EAST);
// 按钮,选择加密方式
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout());
btnDes = new JButton("DES加密");
btnDes.addActionListener(this);
btnCbc = new JButton("CBC加密");
btnCbc.addActionListener(this);
btnCfb = new JButton("CFB加密");
btnCfb.addActionListener(this);
btnOfb = new JButton("OFB加密");
btnOfb.addActionListener(this);
p3.add(btnDes);
p3.add(btnCbc);
p3.add(btnCfb);
p3.add(btnOfb);
// 显示加密密文
JPanel p4 = new JPanel();
p4.setLayout(new FlowLayout());
lblCipher = new JLabel("密文:");
cipherText = new JTextField(20);
cipherText.setEditable(false);
p4.add(lblCipher);
p4.add(cipherText);
// 显示解密明文
JPanel p5 = new JPanel();
p5.setLayout(new FlowLayout());
lblEnCipher = new JLabel("解密結果:");
enCipherText = new JTextField(20);
enCipherText.setEditable(false);
p5.add(lblEnCipher);
p5.add(enCipherText);
// 显示程序运行时间
JPanel p6 = new JPanel();
p6.setLayout(new FlowLayout());
lblRunTime = new JLabel("加密解密过程运行时间:");
runTime = new JTextField(5);
runTime.setEditable(false);
lblUnit = new JLabel("毫秒");
p6.add(lblRunTime);
p6.add(runTime);
p6.add(lblUnit);
// 添加控件至主面板
this.add(p1);
this.add(p2);
this.add(ps);
this.add(p3);
this.add(p4);
this.add(p5);
this.add(p6);
this.setLocation(400, 300);
this.setSize(400, 280);
this.setTitle("4种DES加密方式");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// 按钮事件监听函数
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String algorithm = "DES/ECB/Nopadding";// DES加密模式参数
String message = "";// 欲加密字符串
String keyString = "";// 秘钥
String iv = "";// 初始种子
long time = 0;// 加密解密时间
// 判断是否输入了加密字符串或密钥
if (msg.getText().equals("") || keyStr.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请检查是否输入了加密字符串或密钥");
return;
}
message = msg.getText();
keyString = keyStr.getText();
iv = seed.getText();
// DES加密
if (e.getActionCommand().equals("DES加密")) {
time = System.currentTimeMillis();
DesEn de = new DesEn(algorithm, keyString, message.getBytes());
byte[] msgDes = de.CipherMsg();
cipherText.setText(new String(msgDes));
enCipherText.setText(new String(de.EncipherMsg(msgDes, keyString))
.trim());
time = System.currentTimeMillis() - time;
runTime.setText("" + time);
} else {
// 非原始DES加密,判断是否输入初始种子
if (seed.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请检查是否输入了初始种子");
return;
}
// CBC加密
if (e.getActionCommand().equals("CBC加密")) {
time = System.currentTimeMillis();
CbcEn de = new CbcEn
- 1
- 2
- 3
- 4
前往页