title: Using triple des(3des) of decrypt in java,get error of"javax.crypto.IllegalBlockSizeException: last block incomplete in decryption"
body: <p>I use the code like <a href="https://stackoverflow.com/questions/20227/how-do-i-use-3des-encryption-decryption-in-java">3des-encryption-decryption-in-java</a>,but when I use decrypt it,it got a error like this </p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
  07-17 11:27:27.580: WARN/System.err(22432): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:705)
  07-17 11:27:27.580: WARN/System.err(22432): at javax.crypto.Cipher.doFinal(Cipher.java:1111)
  </code></pre>
  
  <p>But if I change <code>final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");</code> to <code>final Cipher decipher = Cipher.getInstance("DESede/CFB/NoPadding");</code>,the method can run but got a wrong result (the mode is different from server).
  So I want to know the reason about it.  </p>
  
  <p>The decrypt method:</p>
  
  <pre><code>public static String decrypt(byte[] message) throws Exception {
      final MessageDigest md = MessageDigest.getInstance("SHA-1");
      final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
      final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
      for (int j = 0, k = 16; j &lt; 8;) {
          keyBytes[k++] = keyBytes[j++];
      }
  
      final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
      final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
      final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      // final Cipher decipher = Cipher.getInstance("DESede/CFB/NoPadding");
      decipher.init(Cipher.DECRYPT_MODE, key, iv);
      final byte[] plainText = decipher.doFinal(message);
      return new String(plainText, "UTF-8");
  }
  </code></pre>
  
  <p>The encrypt method:</p>
  
  <pre><code>public static byte[] encrypt(String message) throws Exception {
          final MessageDigest md = MessageDigest.getInstance("SHA-1");
          final byte[] digestOfPassword = md.digest(token
                  .getBytes("utf-8"));
          final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
          for (int j = 0, k = 16; j &lt; 8; ) {
              keyBytes[k++] = keyBytes[j++];
          }
  
          final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
          final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
          final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
          cipher.init(Cipher.ENCRYPT_MODE, key, iv, new SecureRandom(new byte[5]));
          cipher.init(Cipher.ENCRYPT_MODE, key, iv);
  
          final byte[] plainTextBytes = message.getBytes("utf-8");
          final byte[] cipherText = cipher.doFinal(plainTextBytes);
          return cipherText;
      }
  </code></pre>