title: AES-256 and PKCS7Padding fails in Java
body: <p>I have a couple of library, C#, PHP and Android where they all encrypt/decrypt a string in the same way so they are all compatible with each other, i.e. C# writes and encrypts data to a database and PHP can successfully decrypt it and return the original string. </p>
  
  <p>I now need to do the same thing with a standard Java application, so I've taken the code from my Android library and need libraries but I am getting an exception. As far as I know the code wasn't Android specific so it shouldn't be a problem. </p>
  
  <p>Below is my encryption function </p>
  
  <pre><code>public static String encrypt(String plainPasword)
      {
              String password = "";
              try
              {
                  SecretKeySpec key = new SecretKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("US-ASCII"), "AES");
                  IvParameterSpec iv = new IvParameterSpec("ryojvlzmdalyglrj".getBytes("US-ASCII"));
  
                  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
  
                  cipher.init(Cipher.ENCRYPT_MODE, key, iv);
  
                  byte[] encoded = cipher.doFinal(plainPasword.getBytes());
                  password = new String(Base64.encodeBase64(encoded));
  
              }
              catch (Exception ex)
              {
                  System.err.println("Encryption Exception: " + ex.toString());
              }
              return password;
      }
  </code></pre>
  
  <p>When I call <code>Encryption.encrypt("myString")</code> I get the following exception:</p>
  
  <pre><code>Encryption Exception: java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/CBC/PKCS7Padding
  </code></pre>
  
  <p>As I said this code is working fine on Android and it shouldn't make any difference where it is running from. </p>
  
  <h2>Update</h2>
  
  <p>I found that I needed PKCS5Padding instead of 7 thanks to a link on a comment. I am now though getting the following exception:</p>
  
  <pre><code>Encryption Exception: java.security.InvalidKeyException: Illegal key size
  </code></pre>