Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I’;我试图在GUI而不是控制台中设置结果_Java_User Interface_Encryption_Netbeans - Fatal编程技术网

Java I’;我试图在GUI而不是控制台中设置结果

Java I’;我试图在GUI而不是控制台中设置结果,java,user-interface,encryption,netbeans,Java,User Interface,Encryption,Netbeans,控制台中的代码是这样的:现在我希望用户在JTextField中输入文本,键将是JButton中的值,输出是另一个JTextField。用户将单击按钮对文本进行加密。还有三把钥匙。但是可以为每个键设置代码,而不是为此设置新按钮并使用if-else。有谁能帮我解决字节的问题吗 BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Text t

控制台中的代码是这样的:现在我希望用户在JTextField中输入文本,键将是JButton中的值,输出是另一个JTextField。用户将单击按钮对文本进行加密。还有三把钥匙。但是可以为每个键设置代码,而不是为此设置新按钮并使用if-else。有谁能帮我解决字节的问题吗

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Text that you want to encrypt:");
String plainText = userInput.readLine();
System.out.println("Enter the key:");
String key = userInput.readLine();
byte[] encrypted = MARS.encrypt(plainText.getBytes(), key.getBytes())
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted Text: " + new String(encrypted));

下面是一个简单的Swing应用程序示例,它在按下按钮后使用键对字符串进行加密。这是相当多的代码,但它应该创建一个如下窗口:

在结果文本字段中显示加密字节数组之前,我正在使用Base64

代码如下:

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import java.awt.*;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.stream.IntStream;

public class EncryptFrame {

    private static final Dimension DEFAULT_FIELD_DIM = new Dimension(300, 20);

    private static JTextField textField;

    private static JTextField keyField;
    private static JTextField resultField;

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Encrypter");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = createPanel();
        JButton encryptButton = new JButton("Encrypt");
        final Container contentPane = frame.getContentPane();
        contentPane.add(panel, BorderLayout.CENTER);
        contentPane.add(encryptButton, BorderLayout.SOUTH);
        encryptButton.addActionListener(e -> {
            String toEncrypt = textField.getText().trim();
            String key = keyField.getText().trim();
            if (!toEncrypt.isEmpty() && !key.isEmpty()) {
                try {
                    byte[] res = encrypt(key.getBytes("UTF-8"), toEncrypt.getBytes("UTF-8"));
                    resultField.setText(Base64.getEncoder().encodeToString(res));
                } catch (UnsupportedEncodingException | NoSuchPaddingException | NoSuchAlgorithmException
                        | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e1) {
                    throw new RuntimeException(e1);
                }
            } else {
                JOptionPane.showMessageDialog(frame, "Text or key cannot be empty.");
            }
        });

        textField.requestFocusInWindow();

        //Display the window.
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JPanel createPanel() {
        JPanel panel = new JPanel();
        GridBagLayout grid = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        addTextField(panel, grid, gbc);
        addKeyField(panel, gbc);
        addResultField(panel, gbc);
        return panel;
    }

    private static void addResultField(JPanel panel, GridBagConstraints gbc) {
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        resultField = new JTextField();
        panel.add(resultField, gbc);
    }

    private static void addKeyField(JPanel panel, GridBagConstraints gbc) {
        gbc.gridx = 0;
        gbc.gridy = 1;
        JLabel keyLabel = new JLabel("Key:");
        panel.add(keyLabel, gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        keyField = new JTextField();
        keyField.setPreferredSize(DEFAULT_FIELD_DIM);
        panel.add(keyField, gbc);
    }

    private static void addTextField(JPanel panel, GridBagLayout grid, GridBagConstraints gbc) {
        panel.setLayout(grid);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.ipadx = 10;
        gbc.ipady = 10;
        gbc.insets = new Insets(3, 3, 2, 2);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        JLabel text = new JLabel("Text:");
        panel.add(text, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        textField = new JTextField();
        textField.setPreferredSize(DEFAULT_FIELD_DIM);
        panel.add(textField, gbc);
    }

    private static byte[] encrypt(byte[] key, byte[] value) throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        byte[] k = new byte[16];
        final boolean lessThanKey = key.length < k.length;
        System.arraycopy(key, 0, k, 0, lessThanKey ? key.length : k.length);
        if (lessThanKey) {
            int diff = Math.abs(key.length - k.length);
            // Pad with 1
            IntStream.range(0, diff).forEach(i -> k[key.length + i] = '1');
        }
        SecretKeySpec skeySpec = new SecretKeySpec(k, "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        return cipher.doFinal(value);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
import javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.spec.SecretKeySpec;
导入javax.swing.*;
导入java.awt.*;
导入java.io.UnsupportedEncodingException;
导入java.security.InvalidKeyException;
导入java.security.NoSuchAlgorithmException;
导入java.util.Base64;
导入java.util.stream.IntStream;
公共类加密框架{
私有静态最终维度默认值\字段\维度=新维度(300,20);
私有静态JTextField textField;
私有静态JTextField键域;
私有静态JTextField resultField;
私有静态void createAndShowGUI(){
//创建并设置窗口。
JFrame=新的JFrame(“加密机”);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel=createPanel();
JButton encryptButton=新JButton(“加密”);
最终容器contentPane=frame.getContentPane();
contentPane.add(面板,BorderLayout.CENTER);
添加(encryptButton,BorderLayout.SOUTH);
encryptButton.addActionListener(e->{
String-toEncrypt=textField.getText().trim();
String key=keyField.getText().trim();
如果(!toEncrypt.isEmpty()&&!key.isEmpty()){
试一试{
byte[]res=加密(key.getBytes(“UTF-8”)、toEncrypt.getBytes(“UTF-8”);
resultField.setText(Base64.getEncoder().encodeToString(res));
}catch(不支持编码异常| NoSuchPaddingException | NoSuchAlgorithmException
|InvalidKeyException | BadPaddingException | IllegalBlockSizeException e1){
抛出新的运行时异常(e1);
}
}否则{
showMessageDialog(框架,“文本或键不能为空”);
}
});
textField.requestFocusInWindow();
//显示窗口。
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
私有静态JPanel createPanel(){
JPanel面板=新的JPanel();
GridBagLayout grid=新的GridBagLayout();
GridBagConstraints gbc=新的GridBagConstraints();
addTextField(面板、网格、gbc);
addKeyField(面板,gbc);
addResultField(面板,gbc);
返回面板;
}
私有静态void addResultField(JPanel面板,GridBagc){
gbc.gridx=0;
gbc.gridy=2;
gbc.gridwidth=2;
resultField=newjtextfield();
面板添加(结果字段,gbc);
}
私有静态void addKeyField(JPanel面板,GridBagc){
gbc.gridx=0;
gbc.gridy=1;
JLabel keyLabel=新的JLabel(“键:”);
面板。添加(键标签,gbc);
gbc.gridx=1;
gbc.gridy=1;
keyField=新的JTextField();
keyField.setPreferredSize(默认字段尺寸);
面板。添加(键域,gbc);
}
私有静态void addTextField(JPanel面板、GridBagLayout网格、GridBagConstraints gbc){
面板布局(网格);
gbc.gridx=0;
gbc.gridy=0;
gbc.ipadx=10;
gbc.ipady=10;
gbc.插图=新插图(3,3,2,2);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1.0;
JLabel text=新的JLabel(“text:”);
面板。添加(文本,gbc);
gbc.gridx=1;
gbc.gridy=0;
textField=新的JTextField();
textField.setPreferredSize(默认字段尺寸);
panel.add(文本字段,gbc);
}
私有静态字节[]加密(字节[]密钥,字节[]值)引发NoSuchPaddingException,NoSuchAlgorithmException,
InvalidKeyException、BadPaddingException、IllegalBlockSizeException{
字节[]k=新字节[16];
最终布尔值lessThanKey=key.lengthk[key.length+i]=“1”);
}
SecretKeySpec skeySpec=新的SecretKeySpec(k,“AES”);
Cipher Cipher=Cipher.getInstance(“AES/CBC/PKCS5PADDING”);
cipher.init(cipher.ENCRYPT_模式,skeySpec);
返回cipher.doFinal(值);
}
公共静态void main(字符串[]args){
//为事件调度线程计划作业:
//创建并显示此应用程序的GUI。
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}