使用eclipse WindowBuilder和JCA-JCE方法的问题

问题描述 投票:0回答:1

我正在自己做一个关于使用JCA-JCE和WindowBuilder作为GUI的加密/解密消息的独立项目。

我的问题是当密码必须加密消息并将其转换为密文时,每次都会出现异常

javax.crypto.IllegalBlockSizeException:输入长度不是16个字节的倍数

我首先尝试没有GUI,一切顺利

现在我将向您展示代码,但请不要因为代码的混乱而对我不礼貌大声笑它只是一个“原型”

感谢所有能帮助我的人

    import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.awt.event.ActionEvent;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.awt.event.ActionEvent;

public class GUI2 extends JFrame {

    private JPanel contentPane;
    private JTextField txtInsert;


    static Cipher ecipher;
    static SecretKey key;
    static byte[] mex;
    static String alg;
    int index;
    static byte[] ciphertext;
    String uno=new String("k");
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI2 frame = new GUI2();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws UnsupportedEncodingException 
     */
    public GUI2() throws UnsupportedEncodingException {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 602, 408);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblInserisciTesto = new JLabel("Inserisci Testo");
        lblInserisciTesto.setBounds(29, 33, 190, 14);
        contentPane.add(lblInserisciTesto);

        /////////////////TEXTFIELD////////////////
        txtInsert = new JTextField();
        mex=uno.getBytes("UTF-8");
        txtInsert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            try {
                mex=txtInsert.getText().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            }
        });{
        txtInsert.setText("INSERT");
        txtInsert.setBounds(276, 30, 284, 20);
        contentPane.add(txtInsert);
        txtInsert.setColumns(10);


        }


        ///////////////LABEL SELEZIONA ALGORITMO/////////////
        JLabel lblSelezionaAlgoritmo = new JLabel("Seleziona Algoritmo");
        lblSelezionaAlgoritmo.setBounds(29, 109, 104, 14);
        contentPane.add(lblSelezionaAlgoritmo); 





        ///////////////BUTTON//////////////////////////////
        JButton btnCrypt = new JButton("CRYPT");{
        btnCrypt.setBounds(223, 316, 89, 23);
        contentPane.add(btnCrypt);


        btnCrypt.addActionListener(new ActionListener() {     /////ACTION
            public void actionPerformed(ActionEvent e) {
                if((index>=0)&&(index<4)) {
                try {
                    SecureRandom random=new SecureRandom();
                    KeyGenerator keygen=KeyGenerator.getInstance("AES");
                    keygen.init(256,random);
                    key=keygen.generateKey();
                    ecipher=Cipher.getInstance(alg);
                    ecipher.init(Cipher.ENCRYPT_MODE,key);
                    ciphertext = ecipher.doFinal(mex);
                    System.out.println(ciphertext);

                } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException  e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                }else if((index>=4)&&(index<7)) {

                }

            }
        });
        }
        //////////////COMBOBOX//////////////////
        JComboBox comboBox = new JComboBox();
        comboBox.setBounds(276, 105, 284, 22);

        String a=new String("AES/CBC/NoPadding");
        String b=new String("AES/CBC/PKCS5Padding");
        String c=new String("AES/ECB/NoPadding");
        String d=new String("AES/ECB/PKCS5Padding");
        String f=new String("DES/CBC/PKCS5Padding");
        String g=new String("DES/ECB/NoPadding");
        String h=new String("DES/ECB/PKCS5Padding");

        comboBox.addItem(a);
        comboBox.addItem(b);
        comboBox.addItem(c);
        comboBox.addItem(d);
        comboBox.addItem(f);
        comboBox.addItem(g);
        comboBox.addItem(h);
        contentPane.add(comboBox);

        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                alg=(String) comboBox.getSelectedItem();
                index=comboBox.getSelectedIndex();
            }
        });{


        }




        JLabel lblCiphertext = new JLabel("CipherText");
        lblCiphertext.setBounds(29, 204, 104, 14);
        contentPane.add(lblCiphertext);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBounds(276, 195, 284, 23);
        contentPane.add(lblNewLabel);
    }
}
java encryption windowbuilder
1个回答
0
投票

你有这个清单:

    String a=new String("AES/CBC/NoPadding");
    String b=new String("AES/CBC/PKCS5Padding");
    String c=new String("AES/ECB/NoPadding");
    String d=new String("AES/ECB/PKCS5Padding");
    String f=new String("DES/CBC/PKCS5Padding");
    String g=new String("DES/ECB/NoPadding");
    String h=new String("DES/ECB/PKCS5Padding");

如果使用NoPadding的密码,则需要提供与加密密码的块大小对齐的数据。如果您指定填充,提供商将确保您的数据正确填充,因此您将避免使用javax.crypto.IllegalBlockSizeException

© www.soinside.com 2019 - 2024. All rights reserved.