I'm trying to use java to decrypt the content encrypted through php, but my java code does not work properly. This is the code for the encryption process.
<?php
function aes_gcm_decrypt($content, $secret) {
$cipher = 'aes-128-gcm';
$ciphertextwithiv = bin2hex(base64_decode($content));
$iv = substr($ciphertextwithiv, 0, 24);
$tag = substr($ciphertextwithiv , -32, 32);
$ciphertext = substr($ciphertextwithiv, 24, strlen($ciphertextwithiv) - 24 - 32);
$skey = hex2bin($secret);
return openssl_decrypt(hex2bin($ciphertext), $cipher, $skey, OPENSSL_RAW_DATA, hex2bin($iv), hex2bin($tag));
}
function aes_gcm_encrypt($data, $secret) {
$cipher = 'aes-128-gcm';
$string = is_array($data) ? json_encode($data) : $data;
$skey = hex2bin($secret);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher));
$tag = NULL;
$content = openssl_encrypt($string, $cipher, $skey, OPENSSL_RAW_DATA, $iv, $tag);
$str = bin2hex($iv) . bin2hex($content) . bin2hex($tag);
return base64_encode(hex2bin($str));
}
$content = 'Test text.{123456}';
$secret = '544553544B4559313233343536';
$encryptStr = aes_gcm_encrypt($content, $secret);
print_r("encrypt -> $encryptStr \n");
print_r("\n");
$decryptStr = aes_gcm_decrypt($encryptStr, $secret);
print_r("decrypt -> $decryptStr \n");
result:
encrypt -> Fun3yZTPcHsxBpft+jBZDe2NjGNAs8xUHY21eZswZE4iLKYdBsyER7RwVfFvuQ==
decrypt -> Test text.{123456}
And then this is not my code, so I can't modify it.
Here is the code I'm having for now on my Java side:
import java.security.spec.KeySpec;
import java.util.Base64;
import java.util.Random;
import javax.crypto.*;
import javax.crypto.spec.*;
public class MyTest {
public static void main(String[] args) throws Exception {
String secret = "544553544B4559313233343536";
String encryptStr = "Fun3yZTPcHsxBpft+jBZDe2NjGNAs8xUHY21eZswZE4iLKYdBsyER7RwVfFvuQ==";
String decryptString = decrypt(encryptStr, secret, 16);
System.out.println("decryptString: " + decryptString);
}
private static String decrypt(String data, String mainKey, int ivLength) throws Exception {
final byte[] encryptedBytes = Base64.getDecoder().decode(data.getBytes("UTF8"));
final byte[] initializationVector = new byte[ivLength];
System.arraycopy(encryptedBytes, 0, initializationVector, 0, ivLength);
SecretKeySpec secretKeySpec = new SecretKeySpec(generateSecretKeyFromPassword(mainKey, mainKey.length()), "AES");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, initializationVector);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
return new String(cipher.doFinal(encryptedBytes, ivLength, encryptedBytes.length - ivLength), "UTF8");
}
private static byte[] generateSecretKeyFromPassword(String password, int keyLength) throws Exception {
byte[] salt = new byte[keyLength];
new Random(password.hashCode()).nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
return factory.generateSecret(spec).getEncoded();
}
}
This java code will throw an AEADBadTagException.
Exception in thread "main" javax.crypto.AEADBadTagException: Tag mismatch!
at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:620)
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1116)
at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2226)
at MyTest.decrypt(MyTest.java:24)
at MyTest.main(MyTest.java:12)
I have searched a lot of information, but still can not solve.
Can you guys possibly help me with building a Java code, that returns the same results?
Thanks in advance!!