title: padding exception and decryption in java
body: <p>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 :</p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
  </code></pre>
  
  <p>and when i pad my byte array with zeros like </p>
  
  <pre><code>temparray[87] = 00;
  </code></pre>
  
  <p>i get an error :</p>
  
  <pre><code>javax.crypto.BadPaddingException: Given final block not properly padded
  </code></pre>
  
  <p>following is my code :</p>
  
  <pre><code>if ((len = inputStream.read(mainBuffer)) &gt; -1) {
                  totalLength = len;
              }
  if (totalLength &gt; 0) {
                  byteToAscii = function.byteToAscii(mainBuffer, totalLength);
              }
  if (byteToAscii.length() &gt; 0) {
                  completeHexString = function.stringToHex(byteToAscii);               
                  debugInfo = "FRAME RECV.=" + completeHexString;
  /* FRAME RECV.=41ed34a41a9de6d270aa1e1464527e88c8bee66a00cfb308f60c105de81db0f1ce43d8c0b9bc4e8070b5ab8d4d3650b55d23223fc687bb1485945bc3228e9707a7aecda9f90657e0ac009571c6469c58a2cd9793cc433ccb5993f2*/
              }
  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");
  dekey = mySecretKeyFactory.generateSecret(myKeySpec);
  byte[] zeros = {0, 0, 0, 0, 0, 0, 0, 0};
  IvParameterSpec iv = new IvParameterSpec(zeros);
  Cipher c = Cipher.getInstance("TripleDES/CBC/PKCS5Padding");
  c.init(Cipher.DECRYPT_MODE, key, iv);
  byte[] decordedValue = new BASE64Decoder().decodeBuffer(completeHexString);
  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>i am new in java cryptography. pl tell me how to do it?
  thanks in advance.</p>