title: AES 256-bit gcm encryption on PHP side and decryption on JAVA side
body: <p>I am trying to use AES-256 bit GCM encryption, between PHP and Java. The problem is that I receive this exception on Java side on Android:</p>
  
  <pre><code>Decryption Error.!
  javax.crypto.AEADBadTagException: error:1e06e065:Cipher functions:aead_aes_gcm_open:BAD_DECRYPT
  </code></pre>
  
  <p>the code I am using:
  on PHP side</p>
  
  <pre><code>$algo = 'aes-256-gcm';
  $iv   = random_bytes(openssl_cipher_iv_length($algo));
  $key  = "secretKey12345678998765432112345"; // 256 bit 
  $data = "HelloWorld";
  $ciphertext = openssl_encrypt(
   $data,
   $algo,
   $key,
   OPENSSL_ZERO_PADDING,
   $iv,
   $tag
  );
  echo base64_encode($key);
  echo base64_encode($iv);
  echo $ciphertext;
  </code></pre>
  
  <p>and on Java, Android, I am using this code:</p>
  
  <pre><code>static final int GCM_TAG_LENGTH = 16;
  ...
  String decryptedText = null;
      byte[] encodedData = Base64.decode(data, Base64.DEFAULT);
      byte[] decodediv = Base64.decode(iv, Base64.DEFAULT);
      byte[] decodedKey = Base64.decode(key, Base64.DEFAULT);
  
      Log.e("data", new String(encodedData, 0));
      Log.e("iv", new String(decodediv, 0));
      Log.e("key", new String(decodedKey, 0));
      Log.d("test", "test");
  
      SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
  
      try {
          Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
  
          c.init(Cipher.DECRYPT_MODE, originalKey , new GCMParameterSpec(GCM_TAG_LENGTH * 8,decodediv));
          byte[] decodedData = c.doFinal(encodedData);
          decryptedText = new String(decodedData, "UTF-8");
          Log.e("MainActivity", "After decryption : "+ decryptedText);
      } catch (Exception e) {
          Log.e("MainActivity", "Decryption Error.!", e);
      }
      return decryptedText;
  </code></pre>
  
  <p>I am using php code from this page: </p>
  
  <pre><code>zimuel.it/blog/authenticated-encrypt-with-openssl-and-php-7-‌​1
  </code></pre>
  
  <p>I am using php v7.1.4</p>
  
  <p>What could be the exception cause?</p>