Aes gcm nopadding in php

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:

Decryption Error.!
javax.crypto.AEADBadTagException: error:1e06e065:Cipher functions:aead_aes_gcm_open:BAD_DECRYPT

the code I am using: on PHP side

$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;

and on Java, Android, I am using this 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;

I am using php code from this page:

zimuel.it/blog/authenticated-encrypt-with-openssl-and-php-7-‌​1

I am using php v7.1.4

What could be the exception cause?

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class T {
private final static char[] hexArray = "0123456789ABCDEF".toCharArray[];
private static String bytesToHex[byte[] bytes] {
char[] hexChars = new char[bytes.length * 2];
for [ int j = 0; j < bytes.length; j++ ] {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String[hexChars];
}
public static void main[String[] args] throws Exception {
byte[] iv = new byte[]{45,45,23,65,23,76,12,121, 45,45,23,65,23,76,12,121};
String password = "password";
String text = "text";
System.out.println["IV: " + bytesToHex[iv]];
PBEKeySpec pbeKeySpec = new PBEKeySpec[password.toCharArray[], iv, 200_000, 128];
SecretKeyFactory factory = SecretKeyFactory.getInstance["PBKDF2WithHmacSHA256"];
SecretKey pbeKey = factory.generateSecret[pbeKeySpec];
byte[] keyBytes = pbeKey.getEncoded[];
System.out.println["KEY: " + bytesToHex[keyBytes]];
SecretKey key = new SecretKeySpec[keyBytes, "AES"];
GCMParameterSpec gcmSpec = new GCMParameterSpec[128, iv];
Cipher cipher = Cipher.getInstance["AES/GCM/NoPadding"];
cipher.init[Cipher.ENCRYPT_MODE, key, gcmSpec];
byte[] ciphertext = cipher.doFinal[text.getBytes[]];
byte[] result = new byte[iv.length + ciphertext.length];
System.arraycopy[iv, 0, result, 0, iv.length];
System.arraycopy[ciphertext, 0, result, iv.length, ciphertext.length];
System.out.println[bytesToHex[result]];
}
}
-------------------------------------------------------

Chủ Đề