【问题标题】:Adding additional authenticated data to AES-GCM on Android在 Android 上向 AES-GCM 添加其他经过身份验证的数据
【发布时间】:2013-10-31 09:33:32
【问题描述】:

我正在尝试将其他经过身份验证的数据 (AAD) 添加到 Android 上的 AES-GCM。我看到Cipher notes 的Java 7 版本关于使用GCMParameterSpecupdateAAD(...) 方法,但鉴于Android 是基于Java 6 的,我完全没有想法。我使用 Spongycastle 作为加密库

  GCMParameterSpec s = new GCMParameterSpec(...);
  cipher.init(..., s);
  cipher.updateAAD(...);  // AAD

【问题讨论】:

    标签: android encryption spongycastle aes-gcm


    【解决方案1】:

    感谢@andrey - 我找到了一个更完整的示例,也来自BC mailing list

    public void testGCM() {
        try {
            byte iv[] = "123456789012".getBytes();
            byte inMsg[] = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
                    .getBytes();
            byte aad[] = "123456789012123456789012123456789012345678901234567890123456"
                    .getBytes();
            byte key[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".getBytes();
    
            System.out.println("inMsgLen===" + inMsg.length);
    
            // encrypt
            AEADParameters parameters = new AEADParameters(
                    new KeyParameter(key), 128, iv, aad);
            GCMBlockCipher gcmEngine = new GCMBlockCipher(new AESFastEngine());
            gcmEngine.init(true, parameters);
    
            byte[] encMsg = new byte[gcmEngine.getOutputSize(inMsg.length)];
            int encLen = gcmEngine.processBytes(inMsg, 0, inMsg.length, encMsg,
                    0);
            encLen += gcmEngine.doFinal(encMsg, encLen);
    
            System.out.println("encLen===" + encLen);
    
            // decrypt
            gcmEngine.init(false, parameters);
    
            byte[] decMsg = new byte[gcmEngine.getOutputSize(encMsg.length)];
            int decLen = gcmEngine.processBytes(encMsg, 0, encMsg.length,
                    decMsg, 0);
            decLen += gcmEngine.doFinal(decMsg, decLen);
    
            System.out.println("decLen===" + decLen);
    
            System.out.println("MSG===" + new String(decMsg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      【解决方案2】:

      来自 BC mailing list:

      我们的疏忽似乎意味着 JCE 提供者没有 目前公开了一种设置 AAD 的机制(也适用于其他 AEAD 密码:CCM、EAX)。

      在轻量级 API 中,AAD 通过 AEADParameters 的实例(字段“关联文本”)。

      使用轻量级 API,您还可以通过 AEADBlockCipher 接口公开的 processAADBytes() 方法提供 AAD 数据。

      【讨论】:

        猜你喜欢
        • 2018-08-20
        • 2016-08-17
        • 2013-07-17
        • 2017-12-15
        • 2019-09-27
        • 2020-09-28
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        相关资源
        最近更新 更多