AES GCM实现与Java身份验证标签 [英] AES GCM implementation with authentication Tag in Java

查看:1610
本文介绍了AES GCM实现与Java身份验证标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AES GCM认​​证在我的Andr​​oid项目,它工作正常。但得到与认证标签的一些问题,当它与OpenSSL的API比较生成的代码。请在下面找到了Java code:

  SecretKeySpec skeySpec =新SecretKeySpec(关键,AES);
字节[] IV = generateRandomIV();
IvParameterSpec ivspec =新IvParameterSpec(IV);
密密码= Cipher.getInstance(AES / GCM / NoPadding);
cipher.init(Cipher.ENCRYPT_MODE,skeySpec,ivspec);
INT outputLength = cipher.getOutputSize(data.length); // prepare输出缓冲
字节[] =输出新的字节[outputLength]
INT outputOffset = cipher.u​​pdate(数据,0,data.length,输出,0); //产生密文
outputOffset + = cipher.doFinal(输出,outputOffset)按;

我使用OpenSSL,以便同在iOS和使用下面code生成认证标签

  NSMutableData *标记= [NSMutableData dataWithLength:标记大小]。
EVP_C​​IPHER_CTX_ctrl(安培; CTX,EVP_C​​TRL_GCM_GET_TAG,[标签长度],[标签mutableBytes])

在Java或充气城堡,无法得到确切的验证标签你可以帮我解决这个问题,它OpenSSL的回归。谢谢


解决方案

在Java中的标签的可惜的补充以密文的结尾。您可以配置大小(位,用8的倍数),使用 GCMParameterSpec 。因此,您可以用抓住它 Arrays.copyOfRange(密文,ciphertext.length - (标记大小/ Byte.SIZE),ciphertext.length)如果你真的想。

这是不幸的,因为标签不的有无的在年底正式,这让食堂GCM的解密的的在线本质 - 要求进行内部缓冲,而不是能够直接返回明文。另一方面,标签被自动解密期间验证

I'm using AES GCM authentication in my android project and it works fine. But getting some issues with authentication tag when it compare with openssl API generate 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 am using openssl for the same in iOS and generating authentication tag using below code

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

In java or bouncy castle, not able to get the exact authentication tag which openssl return and can you help me to resolve this issue. Thanks

解决方案

In Java the tag is unfortunately added at the end of the ciphertext. You can configure the size (in bits, using a multiple of 8) using GCMParameterSpec. You can therefore grab it using Arrays.copyOfRange(ciphertext, ciphertext.length - (tagSize / Byte.SIZE), ciphertext.length) if you really want to.

It is unfortunate since the tag does not have to be put at the end, and it makes messes up the online nature of GCM decryption - requiring internal buffering instead of being able to directly return the plaintext. On the other hand, the tag is automatically verified during decryption.

这篇关于AES GCM实现与Java身份验证标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆