title: javax.crypto.BadPaddingException:Given final block not properly padded
body: <p>I have to <em>decrypt</em> a frame on my server. Encrypted frame comes from client device through GPRS on socket.<em>Encryption</em> is done with <strong>TripleDes</strong> and with a given key.I use same algorithm and key on server side. Frame is a combination of Hex and Ascii String. Now the  problem is : When I pad my byte array with zeros I get the following exception.</p>
  
  <pre><code>javax.crypto.BadPaddingException: Given final block not properly padded
  </code></pre>
  
  <p>Following is my code :</p>
  
  <pre><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";
      }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);
  </code></pre>
  
  <p>Here are the functions which I am using inside the code:</p>
  
  <pre><code>    public String stringToHex(String base) {
              StringBuffer buffer = new StringBuffer();
              int intValue = 0;
              for (int x = 0; x &lt; 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 &lt; length; i++) {
                  returnString += (char) (b[i] &amp; 0xff);
              }
              return returnString;
          }
  </code></pre>
  
  <p>this is the code in c used for encryption at client side.</p>
  
  <pre><code>#include &lt;svc_sec.h&gt;
  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(&amp;Init_Vector[0], '\0', sizeof(Init_Vector));
  memset(Test_Key_Arr, '0', sizeof(Test_Key_Arr));
  memcpy(Test_Key_Arr, &amp;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 &lt; Count_Input_Data)
  {
  Temp_Byte_Count = Count_Input_Data- Counter_Input_Data;
  if(Temp_Byte_Count &gt; 8)
  Temp_Byte_Count = 8;
  
  memcpy(Temp_Input_Frame, &amp;Test_Input_Data[48+Counter_Input_Data], Temp_Byte_Count);
  //succeeding bytes to be 0
  if(Temp_Byte_Count &lt; 8)
  {
  memset(&amp;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 *)&amp;Init_Vector[0];
  for(Counter_Init_Vector =0;Counter_Init_Vector &lt; 8; Counter_Init_Vector++)
  *Temp_Dst_Ptr++ ^= *Temp_Src_Ptr++;
  //============Initializing data ends
  
  DES(DESE, (unsigned char *)&amp;Test_Key_Arr[0],
  (unsigned char *)&amp;Temp_Input_Frame[0], (unsigned char *)&amp;Temp_Output_Frame[0]);
  //DES(TDES3KE, (unsigned char *)&amp;Test_Key_Arr[0],
  // (unsigned char *)&amp;Temp_Input_Frame[0], (unsigned char *)&amp;Temp_Output_Frame[0]);
  Display_Data("AFTER DES::::", Temp_Output_Frame, Temp_Byte_Count);
  
  memcpy(&amp;Test_Output_Data[48+Counter_Input_Data], Temp_Output_Frame, Temp_Byte_Count);
  Counter_Input_Data += Temp_Byte_Count;
  
  if(Counter_Input_Data &lt; Count_Input_Data)
  {
  memcpy(Init_Vector, Temp_Output_Frame, 8);
  
  }
  }
  
  {
  memset(Test_Input_Data, '\0', Len_Input_Data);
  memcpy(&amp;Test_Input_Data[0], &amp;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;
  }
  </code></pre>
  
  <p>I am new in java <code>Cryptography</code>. Please tell me how to do it? Can anybody post the code which can work properly to decrypt my frame. Thanks in advance.</p>