我们计划使用BouncyCastle v1.51在Java中使用"AES/GCM/NoPadding"
。有人可以了解有关使用情况的理想实施/最佳实践吗?生成额外的认证数据(AAD)&身份验证标签?
以下是加密代码:
private static byte[] encryptGCM(byte[] plaintext,
byte[] randomKeyBytes, byte[] randomIvBytes) throws Exception{
SecretKey randomKey = new SecretKeySpec(randomKeyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, randomKey, new IvParameterSpec(
randomIvBytes)); //TODO: here IvParamSpec could also be gcmP = new GCMParameterSpec(12, keys, 32, 12);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, cipher);
cipherOutputStream.write(plaintext);
cipherOutputStream.close();
return byteArrayOutputStream.toByteArray();//this is the encrypted text
}
答案 0 :(得分:2)
我将按顺序回答问题:
doFinal
上,从密文中获取正确数量的字节并将其解释为标记,并输出明文的最后部分。请注意,密文的缓冲是特定于实现的,但是Cipher
的定义方式,某些缓冲已经发生。