i have to decrypt a frame on my server. encrypted frame is coming from client device through GPRS on socket.encryption is done with "TripleDes" and with a given key.same algorithm and key i am using n server side. frame is a combination of Hex and Ascii String. problem is when i decrypt this frame i get an error :

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

and when i pad my byte array with zeros
i get an error :

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

following is my code :

    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";
    --up to---
    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);

here are the functions which i am using inside the code:

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;
    }

i am new in java cryptography. pl tell me how to do it? thanks in advance.

Recommended Answers

All 2 Replies

hmm i havent tried your code, but back when i was doing my own server client encryption, i realizd that if i never converted the data to base64 and sent it over the socket it would decode incorrectly, i think due to symbols etc.... so i first encrypted the data, then coded to to base64, sent it over the socket, decoded it from base64 and then decrypted it, thus prevented a loss of data. http://www.rgagnon.com/javadetails/java-0598.html.

[edit] however if your code does not even work normally ie without sending it over the socket and decrypting but rather doing it on your home pc then then the above will not apply to your problem. only if the problem occurs when you send it over the socket for decryption does the above apply.

here are a few links that might help then: http://eternusuk.blogspot.com/2008/09/java-triple-des-example.html and http://www.java2s.com/Code/Java/Security/TripleDES.htm and http://orlingrabbe.com/3des2_cbc.htm

this is the client side code which is written in c. its not in my hand so i can't encode it with base64.

#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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.