我们计划在Java中使用"AES/GCM/NoPadding",使用BouncyCastle v1.51。有人能说明一些关于额外认证数据(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
}发布于 2014-12-08 20:19:11
我会按顺序回答这些问题:
doFinal上,从密文中提取正确的字节数,并将其解释为标记,并输出明文的最后一部分。请注意,密文的缓冲是特定于实现的,但是按照定义Cipher的方式,必须进行一些缓冲。https://stackoverflow.com/questions/27361148
复制相似问题