javax.crypto.BadPaddingException :Given final block not properly padded

标签 java encryption padding

我必须解密我服务器上的一个帧。加密帧通过套接字上的 GPRS 来自客户端设备。加密 是使用 TripleDes 和给定 key 完成的。我在服务器端使用相同的算法和 key 。 Frame 是 Hex 和 Ascii String 的组合。现在的问题是:当我用零填充字节数组时,出现以下异常。

javax.crypto.BadPaddingException: Given final block not properly padded

以下是我的代码:

byte[] key = new byte[]{31, 30, 31, 36, 32, 11, 11, 11, 22, 26,
               30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30};
myKeySpec = new DESedeKeySpec(key);
mySecretKeyFactory = SecretKeyFactory.getInstance("TripleDES");
de = mySecretKeyFactory.generateSecret(myKeySpec);

    Cipher c = Cipher.getInstance("TripleDES");
c.init(Cipher.DECRYPT_MODE, key);

    int l = completeHexStr.length();

    if (l%8==1){
        completeHexStr = completeHexStr + "0000000";
    }else if (l%8==7){
        completeHexStr = completeHexStr + "0";
    }
byte decordedValue[] =completeHexString.getBytes();
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
System.out.println("decryptedValue= " + decryptedValue);

以下是我在代码中使用的函数:

    public String stringToHex(String base) {
            StringBuffer buffer = new StringBuffer();
            int intValue = 0;
            for (int x = 0; x < base.length(); x++) {
                intValue = base.charAt(x);
                String hex = Integer.toHexString(intValue);
                if (hex.length() == 1) {
                    buffer.append("0" + hex + "");
                } else {
                    buffer.append(hex + "");
                }
            }
            return buffer.toString();
        }
    public String byteToAscii(byte[] b, int length) {
            String returnString = "";
            for (int i = 0; i < length; i++) {
                returnString += (char) (b[i] & 0xff);
            }
            return returnString;
        }

这是客户端加密用的c代码。

#include <svc_sec.h>
const unsigned char fixed_key[] = { 0x31, 0x30, 0x31, 0x36, 0x32, 0x11, 0x11, 0x11, 0x22, 0x26, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30};
int Comm_Encrypt_Data(unsigned char *Test_Input_Data, int Len_Input_Data)
{
int Count_Input_Data, Counter_Input_Data;
unsigned long Timer_1;
unsigned char Init_Vector[8];
int Counter_Init_Vector, Temp_Byte_Count;
unsigned char *Temp_Dst_Ptr, *Temp_Src_Ptr;
unsigned char Temp_Input_Frame[9], Temp_Output_Frame[9];
unsigned char Test_Output_Data[500];
unsigned char Test_Key_Arr[9];

memset(&Init_Vector[0], '\0', sizeof(Init_Vector));
memset(Test_Key_Arr, '0', sizeof(Test_Key_Arr));
memcpy(Test_Key_Arr, &fixed_key[0], 8);
Test_Key_Arr[sizeof(Test_Key_Arr)-1] = '\0';

Display_Data("KEY: ", Test_Key_Arr, sizeof(Test_Key_Arr)-1);

memset(Test_Output_Data, '\0', sizeof(Test_Output_Data));
memcpy(Test_Output_Data, Test_Input_Data, 48);

Count_Input_Data = Len_Input_Data -48 -3; //minus Data before payload, 3 bytes of '|' and CRC
Counter_Input_Data = 0;
while(Counter_Input_Data < Count_Input_Data)
{
Temp_Byte_Count = Count_Input_Data- Counter_Input_Data;
if(Temp_Byte_Count > 8)
Temp_Byte_Count = 8;

memcpy(Temp_Input_Frame, &Test_Input_Data[48+Counter_Input_Data], Temp_Byte_Count);
//succeeding bytes to be 0
if(Temp_Byte_Count < 8)
{
memset(&Temp_Input_Frame[Temp_Byte_Count], '0', (8-Temp_Byte_Count));

}

Display_Data("InPut Data Before Init",Temp_Input_Frame, Temp_Byte_Count);

//============Initialize the data
Temp_Dst_Ptr = (unsigned char *)Temp_Input_Frame;
Temp_Src_Ptr = (unsigned char *)&Init_Vector[0];
for(Counter_Init_Vector =0;Counter_Init_Vector < 8; Counter_Init_Vector++)
*Temp_Dst_Ptr++ ^= *Temp_Src_Ptr++;
//============Initializing data ends

DES(DESE, (unsigned char *)&Test_Key_Arr[0],
(unsigned char *)&Temp_Input_Frame[0], (unsigned char *)&Temp_Output_Frame[0]);
//DES(TDES3KE, (unsigned char *)&Test_Key_Arr[0],
// (unsigned char *)&Temp_Input_Frame[0], (unsigned char *)&Temp_Output_Frame[0]);
Display_Data("AFTER DES::::", Temp_Output_Frame, Temp_Byte_Count);

memcpy(&Test_Output_Data[48+Counter_Input_Data], Temp_Output_Frame, Temp_Byte_Count);
Counter_Input_Data += Temp_Byte_Count;

if(Counter_Input_Data < Count_Input_Data)
{
memcpy(Init_Vector, Temp_Output_Frame, 8);

}
}

{
memset(Test_Input_Data, '\0', Len_Input_Data);
memcpy(&Test_Input_Data[0], &Test_Output_Data[48], Counter_Input_Data); //1 Separator + 2 CRCs
}
Display_Data("Final Output Frame", Test_Input_Data, Counter_Input_Data);
return Counter_Input_Data;
}

我是 java Cryptography 的新手。请告诉我该怎么做?任何人都可以发布可以正常工作的代码来解密我的框架。提前致谢。

最佳答案

您的代码的主要问题是您使用默认的 PKCS5Padding 进行解密。 "TripleDES" 将在内部生成 "TripleDES/ECB/PKCS5Padding"。这是在 Sun JCE 提供程序中实现的;大多数其他提供商复制此默认值。

您似乎需要零填充,这意味着您应该改用 "DESede/ECB/NoPadding"。之后,您可以使用外部函数来计算纯文本大小(如果不小心,删除零填充可能会删除末尾的零值纯文本)。

其他问题:

  • 尝试在解密前填充数据(你应该unpad数据解密之后)
  • 编码和字符编码问题,例如试图用“0”的字符值进行填充,这很可能是错误的

我指定了 "ECB" 因为我不知道实际使用的模式。如果你能找到的话,你可以用正确的模式和填充算法修改你的问题。如果 ECB 不起作用,您可能还想尝试 CBC 模式。

请注意,除非非常特殊的情况,否则使用 E​​CB 模式并不安全。使用 CBC 和随机 IV 是最低要求。

关于javax.crypto.BadPaddingException :Given final block not properly padded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10427205/

相关文章:

java - JPA 使用 CriteriaUpdate 将元素添加到集合

c - 使用 u_long 填充 C 结构体

html - 按钮填充不起作用

Java:打印带删除线的文本

JavaFX - 如何关闭 ToggleButton 的自动 setSelected(bool) 功能?

java - 如何比较两个以上的字符串?

asp.net - web.config 中的加密连接字符串

javascript - 像 JavaScript CryptoJS 一样在 PHP 中加密

从客户端到服务器的 Java NIO + AES 加密 - ByteBuffer 问题

html - CSS 表格 <td> 填充不能完全移除