title: Difference between JAVA and PHP AES ciphering
body: <p>I need to exchange encrypted data between a JAVA application and a PHP website.
  This is the code that's being used to encrypt data:</p>
  
  <pre><code>private static SecretKeySpec createKeyFromString(String plainKey) {
  
      MessageDigest sha = null;
      byte[] key;
      SecretKeySpec secretKey = null;
  
      try {
  
          key = plainKey.getBytes("UTF-8");
          sha = MessageDigest.getInstance("SHA-1");
          key = sha.digest(key);
          key = Arrays.copyOf(key, 16);
  
          secretKey = new SecretKeySpec(key, "AES");
  
      } catch(NoSuchAlgorithmException e) {
          e.printStackTrace();
      } catch(UnsupportedEncodingException e) {
          e.printStackTrace();
      }
  
      return secretKey;
  
  }
  
  private static String AESCrypt(String password, SecretKeySpec secretKey) {
      try {
  
          Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
          cipher.init(Cipher.ENCRYPT_MODE, secretKey);
          return Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes("UTF-8")));
  
      } catch (Exception e) {
          System.out.println(e.toString());
      }
      return null;
  }
  
  
  public static void main(String[] args) {
  
      System.out.println(Pay.AESCrypt("password", Pay.createKeyFromString("key")));
  
  }
  </code></pre>
  
  <p>The output for this program is:</p>
  
  <pre><code>aRtcSG0H5u9v7xVYClnYIw==
  </code></pre>
  
  <p>I'm trying to replicate this behaviour in PHP using the following code:</p>
  
  <pre><code>function createKeyFromString($key) {
      return substr(hash('sha1', $key), 0, 16);
  }
  
  echo openssl_encrypt("password", "AES-128-ECB", createKeyFromString("key"));
  </code></pre>
  
  <p>But I'm getting this output:</p>
  
  <pre><code>pE3cPGDFjM9vwWZ3EO8xDg==
  </code></pre>
  
  <p>I'm a bit lost as to why I'm getting a different ciphered string. I tried padding the data to encrypt in PHP using this function:</p>
  
  <pre><code>function pkcs5_pad($text) {
      $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
      $pad = $size - (strlen($text) % $size);
      return $text . str_repeat(chr($pad), $pad);
  }
  
  echo openssl_encrypt(pkcs5_pad("password"), "AES-128-ECB", createKeyFromString("key"));
  </code></pre>
  
  <p>But it still doesn't work.</p>