title: Can't fix "Error: Input length must be a multiple of 16 when decrypting with a pad cipher"
body: <p>Ello! I am working a chat application that encrypts it's data through AES/CBC/PKCS5 Padding. It works through the client sending the encrypted message to the server where it is sent back and decrypted. Unfortunately, whenever I decrypt the message, I get the error as follows: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher. The encryption is based of this program :(<a href="http://www.scottjjohnson.com/blog/AesWithCbcExample.java" rel="nofollow">http://www.scottjjohnson.com/blog/AesWithCbcExample.java</a>) which works perfectly fine and I cannot see the difference between my code and that one except for that I must convert from string to byte array. Here is my code code:</p>
  
  <p>Client (Encryption):</p>
  
  <pre><code>String message = textField.getText();
  // generate a key
  KeyGenerator keygen = KeyGenerator.getInstance("AES");
  keygen.init(128);  // To use 256 bit keys, you need the "unlimited strength" encryption policy files from Sun.
  byte[] key = keygen.generateKey().getEncoded();
  SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  
  // build the initialization vector.  This example is all zeros, but it 
  // could be any value or generated using a random number generator.
  byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  IvParameterSpec ivspec = new IvParameterSpec(iv);
  
  // initialize the cipher for encrypt mode
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
  
  // encrypt the message
  byte[] encrypted = cipher.doFinal(message.getBytes());
  System.out.println("Ciphertext: " + encrypted + "\n");
  System.out.println(encrypted);
  out.println(encrypted);
  textField.setText(""); 
  </code></pre>
  
  <p>Server Side:</p>
  
  <pre><code>String input = in.readLine();
  writer.println("MESSAGE " + input);
  </code></pre>
  
  <p>Client (Decryption):</p>
  
  <pre><code>//DECRYPTION
  System.out.println(line);
  line = line.substring(8);
  System.out.println(line);
  
  // generate a key
  KeyGenerator keygen = KeyGenerator.getInstance("AES");
  keygen.init(128);  // To use 256 bit keys, you need the "unlimited strength" encryption policy files from Sun.
  byte[] key = keygen.generateKey().getEncoded();
  SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  
  // build the initialization vector.  This example is all zeros, but it 
  // could be any value or generated using a random number generator.
  byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  IvParameterSpec ivspec = new IvParameterSpec(iv);
  
  // reinitialize the cipher for decryption
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
  
  // decrypt the message
  byte[] decrypted = cipher.doFinal(line.getBytes());
  System.out.println("Plaintext: " + new String(decrypted) + "\n");
  messageArea.append(name + ": " + decrypted + "\n");
  messageArea.setCaretPosition(messageArea.getDocument().getLength());
  </code></pre>