在尝试首次实施AES-GCM时,我们面临着生成AuthenticationTag,加密密码和GCM mac检查最终失败的问题.对于当前的实现tag[]正在填充但byte[] encrypted仍然是空的.因为这cipher.doFinal(data1, offset)给了' mac check in GCM failed'.它似乎是围绕字节数组大小的一些问题,有人可以分享在什么基础上确定输出缓冲区大小?这应该以块的形式完成吗?
任何指向AES-GCM实施的指针/链接都将受到高度赞赏.
以下是我们的实施:
public class GCMTest {
public static void main(String[] args) throws Exception {
//***********************************************************
//Key
byte[] key = MessageDigest.getInstance("MD5").digest("1234567890123456".getBytes("UTF-8"));//this is the random key
//Iv
SecureRandom srand = SecureRandom.getInstance("SHA1PRNG");
byte[] iv = new byte[256];
srand.nextBytes(iv);
//Input
byte[] data="inputPlainText".getBytes();
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(16 * Byte.SIZE, iv);
//***********************************************************
//Encryption
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), gcmParameterSpec);
cipher.updateAAD("MyAAD".getBytes("UTF-8"));
//Encrypted output …Run Code Online (Sandbox Code Playgroud)