package gui;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.UIManager;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import en_decrypt.hill.Hill;
import en_decrypt.hill.Matrix;
import tools.Spectrum;
import tools.Tools;
public class HillGUI{
private JFrame frmHill;
private JTextArea textArea_origin;
private JTable table_KeyMatrix;
private JTextArea textArea_result;
private JTable table_KeyMatrixInverse;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//new HillGUI();
HillGUI window = new HillGUI();
window.frmHill.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public HillGUI() {initialize();}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings("serial")
private void initialize() {
frmHill = new JFrame();
frmHill.setTitle(" Hill算法");
frmHill.setBounds(100, 60, 769, 640);
frmHill.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmHill.getContentPane().setLayout(null);
JLabel label_textOrigin = new JLabel("明文/密文");
label_textOrigin.setFont(new Font("华文楷体", Font.PLAIN, 18));
label_textOrigin.setBounds(31, 30, 99, 31);
frmHill.getContentPane().add(label_textOrigin);
JScrollPane scrollPane_origin = new JScrollPane();
scrollPane_origin.setBounds(140, 30, 578, 110);
frmHill.getContentPane().add(scrollPane_origin);
textArea_origin = new JTextArea();
textArea_origin.setLineWrap(true);
scrollPane_origin.setViewportView(textArea_origin);
textArea_origin.setFont(new Font("华文楷体", Font.PLAIN, 18));
textArea_origin.setColumns(10);
JLabel label_keyMatrix = new JLabel("密钥矩阵");
label_keyMatrix.setFont(new Font("华文楷体", Font.PLAIN, 18));
label_keyMatrix.setBounds(85, 189, 81, 61);
frmHill.getContentPane().add(label_keyMatrix);
table_KeyMatrix = new JTable(3, 3) {
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
{
try {
if((String) aValue!="")
Integer.parseInt((String) aValue);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "只能输入数字!");
return;
}
super.setValueAt(aValue, rowIndex, columnIndex);
}};// 设置表格为3行3列
table_KeyMatrix.setBorder(UIManager.getBorder("ComboBox.border"));
table_KeyMatrix.setFont(new Font("宋体", Font.PLAIN, 18));
table_KeyMatrix.setBounds(278, 189, 120, 120);
table_KeyMatrix.setRowHeight(40);
table_KeyMatrix.setRowSelectionAllowed(false);
frmHill.getContentPane().add(table_KeyMatrix);
JLabel label_textResult = new JLabel("结果");
label_textResult.setFont(new Font("华文楷体", Font.PLAIN, 18));
label_textResult.setBounds(31, 422, 68, 31);
frmHill.getContentPane().add(label_textResult);
JScrollPane scrollPane_result = new JScrollPane();
scrollPane_result.setBounds(140, 421, 578, 110);
frmHill.getContentPane().add(scrollPane_result);
textArea_result = new JTextArea();
textArea_result.setLineWrap(true);
scrollPane_result.setViewportView(textArea_result);
textArea_result.setFont(new Font("华文楷体", Font.PLAIN, 18));
textArea_result.setColumns(10);
JButton btn_encrypt = new JButton("加密");
btn_encrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//检验输入
String plainText = textArea_origin.getText();
if (plainText == null || plainText.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "明文或密文不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
int[][] key = new int[3][3];
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
String str = (String)table_KeyMatrix.getValueAt(i, j);
if(str == null || str.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "密钥矩阵填写有误", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
Integer integer = Integer.parseInt(str);
key[i][j] = integer;
}
}
if(new Matrix(key).getDet(key)!=1){
JOptionPane.showMessageDialog(null, "该密钥矩阵行列式的值不等于1,本程序不保证解密正确性。"
+ "建议使用随机密钥", "警告",JOptionPane.WARNING_MESSAGE);
}
// 执行算法
try {
String result = Hill.encrypt(plainText, key);
textArea_result.setText(result);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "输入的密钥矩阵无法计算", "错误",
JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
});
btn_encrypt.setFont(new Font("华文楷体", Font.PLAIN, 18));
btn_encrypt.setBounds(224, 370, 97, 23);
frmHill.getContentPane().add(btn_encrypt);
JButton button_decrypt = new JButton("解密");
button_decrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//检验输入参数
String plainText = textArea_origin.getText();
if (plainText == null || plainText.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "明文或密文不能为空", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
int[][] key = new int[3][3];
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
String str = (String)table_KeyMatrix.getValueAt(i, j);
if(str == null || str.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "密钥矩阵填写有误", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
Integer integer = Integer.parseInt(str);
key[i][j] = integer;
}
}
if(new Matrix(key).getDet(key)!=1){
JOptionPane.showMessageDialog(null, "该密钥矩阵行列式的值不等于1,本程序不保证解密结果正确性",
"警告",JOptionPane.WARNING_MESSAGE);
}
//执行算法
try {
String result = Hill.decrypt(plainText, key);
textArea_result.setText(result);
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "输入的密钥矩阵无法计算", "错误",
JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
//****//显示密钥矩阵的逆矩阵
Integer[][] inverseMatrix = Tools.toIntegerArray(Hill.inverseMatrix);
Character[] columnNames= {1,2,3};
if(table_KeyMatrixInverse != null)
frmHill.getContentPane().remove(table_KeyMatrixInverse);
table_KeyMatrixInverse = new JTable(inverseMatrix, columnNames);
table_KeyMatrixInverse.setEnabled(false);
table_KeyMatrixInverse.setBorder(UIManager.getBorder("ComboBox.border"));
table_KeyMatrixInverse.setRowHeight(40);
table_KeyMatrixInverse.getColumnModel().getColumn(0).setWidth(40);
table_KeyMatrixInverse.getColumnModel().getColumn(1).setWidth(40);
table_KeyMatrixInverse.getColumnModel().getColumn(2).setWidth(40);
table_KeyMatrixInverse.setFont(new Font("宋体", Font.PLAIN, 18));
table_KeyMatrixInverse.setBounds(598, 189, 120, 120);
frmHill.getContentPane().add(table_KeyMatrixInverse);
frmHill.getContentPane().repaint();
//****//
}
});
button_decrypt.setFont(new Font("华文楷体", Font.PLAIN, 18));
button_decrypt.setBounds(534, 370, 97, 23);
frmHill.getContentPane().add(button_decrypt);
JButton btnClear = new JButton("清除");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_origin.setText("");
textArea_result.setText("");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
table_KeyMatrix.setValueAt("",i,j);
if(table_KeyMatrixInverse!=null)
frmHill.getCont
Hill加密算法Java实现,带图形化界面
需积分: 10 91 浏览量
2018-04-20
19:09:11
上传
评论 2
收藏 54KB ZIP 举报
反向传播
- 粉丝: 1
- 资源: 4
最新资源
- J185-VB一款SOT23封装P-Channel场效应MOS管
- 计算整数各位数字之和,利用java代码实现
- J185-T2B-VB一款SOT23封装P-Channel场效应MOS管
- map20231226Kalmanfilter.ipynb
- J185-T1B-VB一款SOT23封装P-Channel场效应MOS管
- ASME Y14.5-2018 尺寸与公差标注 中文版
- J185-T1B-A-VB一款SOT23封装P-Channel场效应MOS管
- J168-VB一款SOT23封装P-Channel场效应MOS管
- J166-VB一款SOT23封装P-Channel场效应MOS管
- i2c测试程序-linux-如何调试i2c.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈