使用JAVA-使用GUI进行界面设计-进行维吉尼亚密码的解密与加密
使用JAVA-进行维吉尼亚密码的解密与加密-CSDN博客
对上面代码进行改进,并设计了GUI界面
来源于百度百科
维吉尼亚密码_百度百科
运行结果如下:
具体代码:
package com.java.MiMa;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
明文:ATTACKATDAWN
密钥:LEMONLEMONLE
密文:LXFOPVEFRNHR
解密后:ATTACKATDAWN
*/
public class WJMYmmGUI extends JFrame {
// 常量 26
public static final int N = 26;
private JTextField plaintextField;
private JTextField keyField;
private JTextArea ciphertextArea;
private JTextArea decryptedtextArea;
public WJMYmmGUI() {
setTitle("维吉尼亚密码加密解密工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLayout(new GridLayout(4, 2, 10, 10));
setLocationRelativeTo(null);//设置窗体居中
// 明文输入部分
JLabel plaintextLabel = new JLabel("请输入需要操作的文字:");
plaintextField = new JTextField();
add(plaintextLabel);
add(plaintextField);
// 密钥输入部分
JLabel keyLabel = new JLabel("请输入密钥:");
keyField = new JTextField();
add(keyLabel);
add(keyField);
// 加密按钮和密文显示部分
JButton encryptButton = new JButton("加密");
ciphertextArea = new JTextArea();//密文显示
ciphertextArea.setEditable(false);//设置不可编辑
encryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String plaintext = plaintextField.getText().toUpperCase();//读取需要操作的文字,toUpperCase是大写,toLowerCase是小写
String key = keyField.getText().toUpperCase();//读取密钥
//将待处理文字与密钥去除空格
plaintext = plaintext.replace(" ", "").toUpperCase();
key = key.replace(" ", "").toUpperCase();
if(key.length() != plaintext.length()){//判断密钥长度是否与明文长度相同
JOptionPane.showMessageDialog(WJMYmmGUI.this, "密钥长度必须与明文长度相同!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
String ciphertext = encrypt(plaintext, key);//进行加密
ciphertextArea.setText(ciphertext);//设置加密结果显示
}
});
add(encryptButton);
add(new JScrollPane(ciphertextArea));
// 解密按钮和解密结果显示部分
JButton decryptButton = new JButton("解密");
decryptedtextArea = new JTextArea();
decryptedtextArea.setEditable(false);//设置不可编辑
decryptButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String plaintext = plaintextField.getText().toUpperCase();//读取需要操作的文字
String key = keyField.getText().toUpperCase();//读取密钥
//将待处理文字与密钥去除空格
plaintext = plaintext.replace(" ", "").toUpperCase();
key = key.replace(" ", "").toUpperCase();
if(key.length() != plaintext.length()){//判断密钥长度是否与明文长度相同
JOptionPane.showMessageDialog(WJMYmmGUI.this, "密钥长度必须与明文长度相同!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
String decryptedtext = decrypt(plaintext, key);//进行解密
decryptedtextArea.setText(decryptedtext);//设置解密结果显示
}
});
add(decryptButton);
add(new JScrollPane(decryptedtextArea));
setVisible(true);//设置窗体可见
}
// 构建加密矩阵
private char[][] buildEncryptionMatrix() {
String ZM = "abcdefghijklmnopqrstuvwxyz";
char[] zm = ZM.toCharArray();
char[][] zmb = new char[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
zmb[i][j] = (char) (zm[j] + i);
if (zmb[i][j] >= 'a' && zmb[i][j] <= 'z') {
zmb[i][j] = (char) (zmb[i][j] - 32);
} else {
zmb[i][j] = (char) (zmb[i][j] - N);
zmb[i][j] = (char) (zmb[i][j] - 32);
}
}
}
return zmb;
}
// 加密方法
private String encrypt(String plaintext, String key) {
char[][] zmb = buildEncryptionMatrix();
char[] mw = plaintext.toCharArray();
char[] k = key.toCharArray();
char[] miw = new char[mw.length];
for (int i = 0; i < mw.length; i++) {
int h = k[i % k.length] - 65;
int l = mw[i] - 65;
miw[i] = zmb[h][l];
}
return new String(miw);
}
// 解密方法
private String decrypt(String ciphertext, String key) {
char[][] zmb = buildEncryptionMatrix();
char[] miw = ciphertext.toCharArray();
char[] k = key.toCharArray();
char[] jm = new char[miw.length];
for (int i = 0; i < miw.length; i++) {
int h = k[i % k.length] - 65;
for (int j = 0; j < N; j++) {
if (zmb[h][j] == miw[i]) {
jm[i] = zmb[0][j];
break;
}
}
}
return new String(jm);
}
public static void main(String[] args) {
new WJMYmmGUI();
}
}