如何使用 Android 的身份验证标签进行 GCM 加密

Ngu*_*ong 2 encryption android aes-gcm

我想制作一个通过 GCM 模式与 Android 身份验证标签加密数据的函数。这是我的源代码:

public static byte[] GCMEncrypt(String hexKey, String hexIV, byte[] aad) throws Exception {
        byte[] aKey = hexStringToByteArray(hexKey);
        byte[] aIV = hexStringToByteArray(hexIV);
        Key key = new SecretKeySpec(aKey, "AES");
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key, new GCMParameterSpec(16 * Byte.SIZE, aIV));
        cipher.updateAAD(aad);
        byte[] encrypted = cipher.doFinal(aKey);
        return encrypted;
    }
Run Code Online (Sandbox Code Playgroud)

如何将身份验证标签插入此代码中?

Maa*_*wes 5

您已经包含了身份验证标签。Java(不幸的是 - 见下文)在密文中包含身份验证标记。这意味着它是在最后一次调用 期间生成的doFinal

您可以通过查看密文的大小轻松检查这一点。它应该与明文的大小相同(在本例中aKey加上 128 位,这是身份验证标签的默认且合理的大小。

这也是源自AEADBadTagExceptionBadPaddingException在解密过程中,验证是隐式进行的。因此旧代码仍然与 GCM 模式兼容。


正如我之前指出的,我认为在密文中包含身份验证标签是 API 的一个主要设计错误:

  • 它不允许用户将身份验证标签放置在其他地方 - 至少不能进行数组(大小)操作;
  • 它消除了GCM 的在线性质,其中明文解密是即时的(对于每次更新调用),因为身份验证标签需要缓冲;
  • 如果预先知道认证标签的位置(即,如果协议直接指定标签位置或密文的大小),它会使代码更大、效率更低,并且需要虚假缓冲;

在我看来,这不符合在Cipher不添加方法和保持兼容性的情况下改造类的优点。如果设计者只是添加单独检索和验证身份验证标签的方法,那就更好了。


因为目前正在下雨,所以我创建了一个示例:

public static void main(String... args) throws Exception {
    int tagSize = 96;

    Cipher gcm = Cipher.getInstance("AES/GCM/NoPadding");

    SecretKey aesKey = new SecretKeySpec(new byte[16], "AES");

    GCMParameterSpec gcmSpec = new GCMParameterSpec(tagSize, new byte[gcm.getBlockSize()]);

    gcm.init(Cipher.ENCRYPT_MODE, aesKey, gcmSpec);

    byte[] pt = "Maarten Bodewes creates code".getBytes(StandardCharsets.UTF_8);
    System.out.println(pt.length);
    byte[] ctAndTag = new byte[gcm.getOutputSize(pt.length)];

    System.out.println(ctAndTag.length);

    int off = 0;
    off += gcm.update(pt, 0, pt.length, ctAndTag, off);
    // prints 16 (for the Oracle crypto provider)
    // meaning it is not online, buffering even during encryption
    System.out.println(off);
    off += gcm.doFinal(new byte[0], 0, 0, ctAndTag, off);
    // prints 40 for the Oracle crypto provider, meaning it doesn't *just*
    // output the tag during doFinal !
    System.out.println(off);

    int ctSize = ctAndTag.length - tagSize / Byte.SIZE;
    System.out.println(ctSize);

    byte[] ct = Arrays.copyOfRange(ctAndTag, 0, ctSize);
    byte[] tag = Arrays.copyOfRange(ctAndTag, ctSize, ctAndTag.length);

    System.out.println(Hex.toHexString(ct));
    System.out.println(Hex.toHexString(tag));
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 用自身加密密钥没有意义,我希望这只是出于演示目的
  • 对于较小的身份验证标签,GCM 很快就会失去安全性,不建议更改大小,除非您的协议是专门为此设计的。