Implement AES GCM with Tag Authentication in Java

I use AES GCM authentication in my Android project and it works great. But there is a problem with the authentication tag when it compares with the openssl API tag. Please find the java code below:

SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
byte[] iv = generateRandomIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
int outputLength = cipher.getOutputSize(data.length); // Prepare output buffer
byte[] output = new byte[outputLength];
int outputOffset = cipher.update(data, 0, data.length, output, 0);// Produce cipher text
outputOffset += cipher.doFinal(output, outputOffset);

I use openssl for the same in iOS and generate an authentication tag using the code below

NSMutableData* tag = [NSMutableData dataWithLength:tagSize];
EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, [tag length], [tag mutableBytes])

In java or bouncy castle, unable to get the exact authentication tag that openssl returns, and you can help me solve this problem. Thanks

+4
source share
1 answer

Java , , . ( , 8), GCMParameterSpec. , Arrays.copyOfRange(ciphertext, ciphertext.length - (tagSize / Byte.SIZE), ciphertext.length), .

, , - GCM - , . , .

+4

Source: https://habr.com/ru/post/1542002/


All Articles