使用Java中的身份validation标签进行AES GCM实现

我在我的Android项目中使用AES GCM身份validation,它工作正常。 但是当与openssl API比较时,获得authentication标签的一些问题会生成标签。 请find下面的java代码:

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

我在iOS中使用openssl,使用下面的代码生成authentication标签

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

在Java或充气城堡,无法获得openssl返回的确切的身份validation标签,你能帮我解决这个问题。 谢谢

在Java中,标签不幸的被添加在密文的末尾。 您可以使用GCMParameterSpecconfiguration大小(以位为单位,使用8的倍数)。 因此,您可以使用Arrays.copyOfRange(ciphertext, ciphertext.length - (tagSize / Byte.SIZE), ciphertext.length)来抓取它。

这是不幸的,因为标签不一定要放在最后,这使GCM 解密的在线性变得困难 – 需要内部缓冲,而不是直接返回明文。 另一方面,标签在解密期间被自动validation。