当前位置: 首页 > news >正文

使用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();
    }
}

相关文章:

  • 力扣hot100二刷——动态规划
  • 落地长沙市某三甲医院!麒麟信安云桌面再添建设标杆
  • k8s1.22 kubeadm 部署
  • 解决vscode终端和本地终端python版本不一致的问题
  • 音视频 二 看书的笔记 MediaPlayer
  • MySQL 8.0.41源码目录深度解析:探索数据库内核的架构蓝图
  • 利用python调接口获取物流标签,并转成PDF保存在指定的文件夹。
  • SylixOS 中 select 原理及使用分析
  • Keil5 安装全攻略
  • 【django】1-1 django构建web程序的基础知识
  • Photoshop怎样保存为ico格式
  • VS自定义静态库并在其他项目中使用
  • SQL Server安装程序无法启动:系统兼容性检查失败
  • 【计算机网络】计算机网络协议、接口与服务全面解析——结合生活化案例与图文详解
  • 中级:设计模式面试题全解析
  • MQTT之重复消息产生
  • node-ddk,electron,主进程通讯,窗口间通讯
  • Django之旅:第五节--Mysql数据库操作(一)
  • 鸿蒙HarmonyOS NEXT之无感监听
  • CSS rem、vw/vh、less
  • 居委业委居民群策群力,7位一级演员来到上海一小区唱戏
  • 神十九乘组安全顺利出舱
  • 向总书记汇报具身智能发展的“稚辉君”:从期待到兴奋再到备受鼓舞
  • 专访丨青年作家杜梨:以动物的视角去观察这个世界
  • 深圳宝安区一宗涉宅用地中止出让,起始总价86.27亿元
  • 商务部:将积极会同相关部门加快推进离境退税政策落实落地