title: PHP AES encryption PKCS5Padding
body: <p>I'm not a PHP coder so I need a little help with PHP AES encryption.</p>
  
  <p>I'm writing code where I am encrypting image files in PHP and then decrypting them in Java (Android).  Everything works fine when I'm encrypting/decrypting PNG files, but when I try to do the same with JPG, Java decrypting throws an exception:</p>
  
  <pre><code>WARN/System.err(345): java.io.IOException: data not block size aligned
  </code></pre>
  
  <p>Based on searching online, it seems that this is because I am not doing padding properly.</p>
  
  <p>How can I do this properly?</p>
  
  <p>Here is the PHP code for encryption:</p>
  
  <pre><code>&lt;?php
      $secret_key   = "01234567890abcde";
      $iv           = "fedcba9876543210";
      $infile       = "5.png";
      $outfile      = "5_encrypted.png";
  
      $crypttext = file_get_contents($infile);
      $plaintext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $crypttext, MCRYPT_MODE_CBC, $iv);
  
      header('Content-Type: application/octet-stream');
      header('Content-Length: ' . strlen($plaintext));
      header('Content-Disposition: attachment; filename=' . ($outfile));
  
      echo $plaintext;
  
  
      //file_put_contents($outfile,$plaintext);   
      //save the file in the folder of server
  </code></pre>