title: how do i encrypt and decrypt use AES/GCM/ between PHP and JAVA
body: <p>I use PHP to encrypt use AES/GCM to communicate with JAVA，but it does not work。 This is the code。I don't know where is the wrong?</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  $key = &quot;123456789012345678901234567890&quot;;
  $plaintext = &quot;aaaaaaa&quot;;
  $encryptStr = aesGcmEncrypt($plaintext, $key);
  echo &quot;加密后：&quot; . $encryptStr;
  
  
  function aesGcmEncrypt($plaintext, $key)
  {
  
      $ivlen = openssl_cipher_iv_length($cipher = &quot;aes-128-gcm&quot;);
      $iv = openssl_random_pseudo_bytes($ivlen);
      $ciphertext_raw = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_NO_PADDING, $iv, $tag);
      $ciphertext = base64_encode($iv . $ciphertext_raw . $tag);
      return $ciphertext;
  }
  
  function decrypt($str, $key)
  {
      $encrypt = base64_decode($str);
      $ivlen = openssl_cipher_iv_length($cipher = &quot;aes-128-gcm&quot;);
      $tag_length = 16;
      $iv = substr($encrypt, 0, $ivlen);
      $tag = substr($encrypt, -$tag_length);
      $ciphertext = substr($encrypt, $ivlen, -$tag_length);
  
      $ciphertext_raw = openssl_decrypt($ciphertext, $cipher, $key, OPENSSL_NO_PADDING, $iv, $tag);
      return $ciphertext_raw;
  }
  
  </code></pre>
  <p>this is the java code。</p>
  <pre><code>private static String aesGcmEncrypt(String content, byte[] key) {
          try {
              System.out.println(content);
              System.out.println(content.getBytes(UTF_8).length);
              // 根据指定算法ALGORITHM自成密码器
              Cipher cipher = Cipher.getInstance(&quot;AES/GCM/PKCS5Padding&quot;);
              SecretKeySpec skey = new SecretKeySpec(key, &quot;AES&quot;);
              cipher.init(Cipher.ENCRYPT_MODE, skey);
              //获取向量
              byte[] ivb = cipher.getIV();
              byte[] encodedByteArray = cipher.doFinal(content.getBytes(UTF_8));
  
              byte[] message = new byte[ivb.length + encodedByteArray.length];
  
              System.arraycopy(ivb, 0, message, 0, ivb.length);
              System.arraycopy(encodedByteArray, 0, message, ivb.length, encodedByteArray.length);
              String ss = Base64.getEncoder().encodeToString(message);
              return ss;
          } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
                  | BadPaddingException e) {
              return null;
          }
      }
  
  </code></pre>
  <p>JAVA code can't modify,because it is not mine,I must adapt to the java code.</p>