You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助:将控制台加密代码改造为Java GUI程序(多密钥处理)

搞定Java GUI加密程序:多密钥独立实现,告别if-else

我来帮你把控制台加密程序改成符合需求的GUI版本,完全避开if-else,每个密钥对应独立的代码逻辑,同时解决byte[]处理的显示问题。

核心思路

不用通过判断按钮来分支逻辑,给每个密钥按钮绑定独立的ActionListener实现——每个按钮点击时直接触发对应密钥的加密方法,这样每个密钥的代码完全隔离,维护起来也方便。另外,加密后的byte[]直接转字符串会乱码,建议用Base64编码转成可正常显示的字符串。

完整可运行代码

import javax.swing.*;
import java.awt.*;
import java.util.Base64;

public class EncryptionGUI extends JFrame {
    private JTextField inputField;
    private JTextField outputField;

    public EncryptionGUI() {
        // 窗口基础配置
        setTitle("文本加密工具");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 200);
        setLayout(new BorderLayout());

        // 输入区
        JPanel inputPanel = new JPanel(new FlowLayout());
        inputPanel.add(new JLabel("输入文本:"));
        inputField = new JTextField(20);
        inputPanel.add(inputField);
        add(inputPanel, BorderLayout.NORTH);

        // 密钥按钮区
        JPanel buttonPanel = new JPanel(new FlowLayout());
        
        // 密钥1按钮:绑定专属加密逻辑
        JButton key1Btn = new JButton("密钥1");
        key1Btn.addActionListener(e -> encryptWithKey1());
        buttonPanel.add(key1Btn);

        // 密钥2按钮:绑定专属加密逻辑
        JButton key2Btn = new JButton("密钥2");
        key2Btn.addActionListener(e -> encryptWithKey2());
        buttonPanel.add(key2Btn);

        // 密钥3按钮:绑定专属加密逻辑
        JButton key3Btn = new JButton("密钥3");
        key3Btn.addActionListener(e -> encryptWithKey3());
        buttonPanel.add(key3Btn);
        add(buttonPanel, BorderLayout.CENTER);

        // 输出区
        JPanel outputPanel = new JPanel(new FlowLayout());
        outputPanel.add(new JLabel("加密结果:"));
        outputField = new JTextField(20);
        outputField.setEditable(false); // 结果禁止编辑
        outputPanel.add(outputField);
        add(outputPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    // 密钥1对应的加密逻辑(替换成你的真实加密代码)
    private void encryptWithKey1() {
        String input = inputField.getText().trim();
        if (input.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入要加密的文本哦");
            return;
        }

        try {
            // 这里替换成你原来控制台的加密逻辑
            byte[] inputBytes = input.getBytes("UTF-8");
            byte key = (byte) 0x1A; // 示例密钥,换成你的密钥值
            byte[] encryptedBytes = new byte[inputBytes.length];
            for (int i = 0; i < inputBytes.length; i++) {
                encryptedBytes[i] = (byte) (inputBytes[i] ^ key); // 示例异或加密
            }
            // 把字节数组转成Base64字符串,避免乱码
            String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes);
            outputField.setText(encryptedStr);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "加密出错啦: " + ex.getMessage());
        }
    }

    // 密钥2对应的加密逻辑(完全独立的代码块)
    private void encryptWithKey2() {
        String input = inputField.getText().trim();
        if (input.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入要加密的文本哦");
            return;
        }

        try {
            // 这里替换成你原来控制台的加密逻辑
            byte[] inputBytes = input.getBytes("UTF-8");
            byte key = (byte) 0x3F; // 示例密钥,换成你的密钥值
            byte[] encryptedBytes = new byte[inputBytes.length];
            for (int i = 0; i < inputBytes.length; i++) {
                encryptedBytes[i] = (byte) (inputBytes[i] + key); // 示例加密逻辑
            }
            String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes);
            outputField.setText(encryptedStr);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "加密出错啦: " + ex.getMessage());
        }
    }

    // 密钥3对应的加密逻辑(完全独立的代码块)
    private void encryptWithKey3() {
        String input = inputField.getText().trim();
        if (input.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请输入要加密的文本哦");
            return;
        }

        try {
            // 这里替换成你原来控制台的加密逻辑
            byte[] inputBytes = input.getBytes("UTF-8");
            byte key = (byte) 0x5D; // 示例密钥,换成你的密钥值
            byte[] encryptedBytes = new byte[inputBytes.length];
            for (int i = 0; i < inputBytes.length; i++) {
                encryptedBytes[i] = (byte) (inputBytes[i] * key); // 示例加密逻辑
            }
            String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes);
            outputField.setText(encryptedStr);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "加密出错啦: " + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        // 按照Swing规范,在事件调度线程启动GUI
        SwingUtilities.invokeLater(EncryptionGUI::new);
    }
}

重点细节解释

  • 无if-else的独立逻辑:每个按钮直接绑定对应密钥的加密方法,不用判断按钮是谁,代码结构清晰,后续修改某个密钥的逻辑完全不影响其他按钮。
  • byte[]处理方案:加密后的字节数组用Base64编码转成字符串,既不会出现乱码,也能完整保留加密后的字节信息,解密时再用Base64解码还原即可。
  • 用户体验优化:判断输入为空的情况,弹窗提示;加密异常也会弹窗告知,避免程序崩溃。
  • Swing线程安全:用SwingUtilities.invokeLater()启动GUI,符合Swing的线程安全要求,避免潜在的GUI渲染问题。

快速替换你的加密逻辑

你只需要把三个encryptWithKeyX()方法里的示例加密代码,替换成你原来控制台程序中的真实加密逻辑就行。比如原来控制台中处理byte[]的代码,直接复制到对应的方法里,最后转成Base64字符串输出到outputField就搞定了。

内容的提问来源于stack exchange,提问作者ays

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:35:41