首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在EAX中使用JCE检查的AES/EAX密码失败

在EAX中使用JCE检查的AES/EAX密码失败
EN

Stack Overflow用户
提问于 2019-06-17 21:26:11
回答 1查看 590关注 0票数 1

我正在尝试使用AES/EAX/NoPadding执行加密/解密。由于EAX似乎没有BouncyCastle,所以BC被添加为一个提供程序。

当我试图加密"Hello!“时,它似乎已经成功加密了。

代码语言:javascript
复制
@NotNull
@Override
public byte[] encrypt(@NotNull Key key, @NotNull byte[] plain, @Nullable byte[] authentication) throws CryptoException {
    try {
        final AesEaxKey aesEaxKey = (AesEaxKey) key;
        final Cipher cipher = Cipher.getInstance(getCipherAlgorithm(), BouncyCastleProvider.PROVIDER_NAME);
        final byte[] cipherText = new byte[getIvSize(aesEaxKey) + plain.length + getTagSize()];
        final byte[] iv = randomIv(aesEaxKey);

        System.arraycopy(iv, 0, cipherText, 0, getIvSize(aesEaxKey));
        cipher.init(Cipher.ENCRYPT_MODE, aesEaxKey, getParameterSpec(iv));

        if (authentication != null && authentication.length != 0) {
            cipher.updateAAD(authentication);
        }

        cipher.doFinal(plain, 0, plain.length, cipherText, getIvSize(aesEaxKey));
        return cipherText;
    } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | NoSuchProviderException |
            InvalidKeyException | BadPaddingException | IllegalBlockSizeException | ShortBufferException e) {
        throw new CryptoException(e.getMessage(), e);
    }
}

当我试图解密密文时,它会抛出"Mac in EAX“。

代码语言:javascript
复制
@NotNull
@Override
public byte[] decrypt(@NotNull Key key, @NotNull byte[] cipherText, @Nullable byte[] authentication) throws CryptoException {
    try {
        final AesEaxKey aesEaxKey = (AesEaxKey) key;
        final Cipher cipher = Cipher.getInstance(getCipherAlgorithm(), BouncyCastleProvider.PROVIDER_NAME);
        cipher.init(Cipher.DECRYPT_MODE, aesEaxKey, getParameterSpec(cipherText, 0, getIvSize(aesEaxKey)));
        if (authentication != null && authentication.length != 0) {
            cipher.updateAAD(authentication);
        }
        return cipher.doFinal(cipherText, getIvSize(aesEaxKey), cipherText.length - getIvSize(aesEaxKey));
    } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException |
            InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
        throw new CryptoException(e.getMessage(), e);
    }
}

详细信息:

  1. getParameterSpec()返回带有IV的IvParameterSpec实例。
  2. 在加密期间,IV字节被插入到密文byte[]的开头,并在解密期间从密文中检索。
  3. 所使用的标记大小为16字节。
  4. AesEaxKey只是一个包装类,它实现了SecretKey并委托了它的所有方法。

我有一个AES/GCM/NoPadding的实现,它使用相同的代码,非常完美。

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-17 22:45:34

EAX这样的AEAD模式需要更复杂的AlgorithmParameterSpec,因为必须同时指定nonce (又名IV)和以位为单位的标记长度。Java自1.7以来为GCM密码提供了一个GCMParameterSpec。EAX模式需要相同的数据,显然Bouncycastle也将为EAX模式使用GCMParameterSpec。

注意,对于GCMParameterSpec,标记长度以位为单位指定,而为了调整数组的大小,标记长度需要以字节为单位指定。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56639117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档