使用 Eclipse WindowBuilder 和 JCA-JCE 方法的问题
Problem using eclipse WindowBuilder and JCA-JCE methods
我正在自己做一个关于 encryption/decryption 消息的独立项目,使用 JCA-JCE 和 WindowBuilder 作为 GUI。
我的问题是当密码必须加密消息并将其转换为密文时,每次都会出现此异常
javax.crypto.IllegalBlockSizeException: 输入长度不是16字节的倍数
我先在没有 GUI 的情况下尝试了一下,一切正常
现在我将向您展示代码,但请不要因为代码混乱而对我无礼哈哈,这只是一个 "prototype"
感谢所有能帮助我的人
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);
}
}
你有这个列表:
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
我正在自己做一个关于 encryption/decryption 消息的独立项目,使用 JCA-JCE 和 WindowBuilder 作为 GUI。
我的问题是当密码必须加密消息并将其转换为密文时,每次都会出现此异常
javax.crypto.IllegalBlockSizeException: 输入长度不是16字节的倍数
我先在没有 GUI 的情况下尝试了一下,一切正常
现在我将向您展示代码,但请不要因为代码混乱而对我无礼哈哈,这只是一个 "prototype"
感谢所有能帮助我的人
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);
}
}
你有这个列表:
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