1

对于所有仇恨者,我阅读了许多类似的主题,但没有一个是有帮助的。

例如。这里javax.crypto.BadPaddingException: Given final block not proper padded error while decryption or here Given final block not proper padded

我想加密然后解密字符串。阅读有关“未正确填充最终块”异常的许多主题,但这些解决方案均无效。

我的课:

package aes;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JOptionPane;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class EncryptionExample {

private static SecretKeySpec    key;
private static IvParameterSpec  ivSpec;
private static Cipher           cipher; 
private static byte[]           keyBytes;
private static byte[]           ivBytes;
private static int              enc_len;

public static void generateKey() throws Exception
        {
        
            String complex = new String ("9#82jdkeo!2DcASg");
            keyBytes = complex.getBytes();
            key = new SecretKeySpec(keyBytes, "AES");

            complex = new String("@o9kjbhylK8(kJh7"); //just some randoms, for now
            ivBytes = complex.getBytes();
            ivSpec = new IvParameterSpec(ivBytes);

            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        }
        
        public static String encrypt(String packet) throws Exception
        {
            byte[] packet2 = packet.getBytes();
            cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
            byte[] encrypted = new byte[cipher.getOutputSize(packet2.length)];
            enc_len = cipher.update(packet2, 0, packet2.length, encrypted, 0);
            enc_len += cipher.doFinal(encrypted, enc_len);
            
            return packet = new String(encrypted);
        }
        
        public static String decrypt(String packet) throws Exception
        {
            byte[] packet2 = packet.getBytes();
            cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
            byte[] decrypted = new byte[cipher.getOutputSize(enc_len)];
            int dec_len = cipher.update(packet2, 0, enc_len, decrypted, 0);
HERE EXCEPTION>>>>> dec_len += cipher.doFinal(decrypted, dec_len);  <<<<<<<<<
            
            return packet = new String(decrypted);
        }
        
        
        // and display the results
    public static void main (String[] args) throws Exception 
    {
        
          // get the text to encrypt
        generateKey();
        String inputText = JOptionPane.showInputDialog("Input your message: ");
        
        String encrypted = encrypt(inputText);
        String decrypted = decrypt(encrypted);            
                
        JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                    "Encrypted:  " + new String(encrypted) + "\n"
                      +  "Decrypted: : " + new String(decrypted));
          .exit(0);
    }
}

问题是,当我解密字符串(大约 4/10 的镜头)时,我得到了这个异常:

Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:479)
at javax.crypto.Cipher.doFinal(Cipher.java:2068)
at aes.EncryptionExample.deszyfrujBez(EncryptionExample.java:HERE tag)
at aes.EncryptionExample.main(EncryptionExample.java:Main starting)

有人知道要在这里更改什么(键?*.doFinal() 方法?)以使其工作吗?

@对于那些好奇的人-方法必须是静态的,因为这是更大的一部分;)

4

2 回答 2

4

使用时,byte[] packet2 = packet.getBytes()您将根据默认编码转换字符串,例如,可能是 UTF-8。没关系。但是然后您将密文转换回这样的字符串:return packet = new String(encrypted)如果稍后在解密()中与另一个byte[] packet2 = packet.getBytes().

试试这个:return packet = new String(encrypted, "ISO-8859-1")byte[] packet2 = packet.getBytes("ISO-8859-1")-- 这不是我想要的,但它应该往返字节数组。

于 2014-06-05T23:59:38.033 回答
0

加密的结果是二进制数据。在大多数情况下,它不能被解释为有效的字符串编码。所以调用new String(encrypted)很可能会扭曲加密的字节,然后packet.getBytes()你会得到一个包含不同内容的字节数组。

现在解密失败,因为密码文本已更改。填充字节未正确恢复且无法删除。

要解决这个问题,不要将密码文本转换为字符串,保留字节数组。

于 2014-06-05T18:40:09.500 回答