【问题标题】:Random access of encrypted data AES GCM mode随机存取加密数据 AES GCM 模式
【发布时间】:2017-10-25 20:34:41
【问题描述】:

随机访问 AES CTR 模式有一个很好的例子,它可以工作: Random access InputStream using AES CTR mode in android

private static final int AES_BLOCK_SIZE = 16;
private static IvParameterSpec calculateIVForOffset(final IvParameterSpec iv,
    final long blockOffset) {
final BigInteger ivBI = new BigInteger(1, iv.getIV());
final BigInteger ivForOffsetBI = ivBI.add(BigInteger.valueOf(blockOffset
        / AES_BLOCK_SIZE));

final byte[] ivForOffsetBA = ivForOffsetBI.toByteArray();
final IvParameterSpec ivForOffset;
if (ivForOffsetBA.length >= AES_BLOCK_SIZE) {
    ivForOffset = new IvParameterSpec(ivForOffsetBA, ivForOffsetBA.length - AES_BLOCK_SIZE,
            AES_BLOCK_SIZE);
} else {
    final byte[] ivForOffsetBASized = new byte[AES_BLOCK_SIZE];
    System.arraycopy(ivForOffsetBA, 0, ivForOffsetBASized, AES_BLOCK_SIZE
            - ivForOffsetBA.length, ivForOffsetBA.length);
    ivForOffset = new IvParameterSpec(ivForOffsetBASized);
}

return ivForOffset;
}

但是,它不适用于 AES GCM 模式。解密时我得到垃圾。我不是加密专家,并且已经尝试破解它几天了。也许任何人都可以提供任何见解?我猜我需要以某种方式更改偏移量的 IV 计算,或者它与身份验证标签(我没有使用)有关。

【问题讨论】:

    标签: java encryption cryptography aes aes-gcm


    【解决方案1】:

    GCM 模式使用计数器模式来保证机密性。所以无需认证就可以解密密文;看看我的回答here。要从给定的偏移量加密或解密,您可以更改计数器以更改给定偏移量的字节并对结果密文进行异或运算。但是,如果您跳过一个字节,您将无法验证任何密文。

    因此,使用 GCM 最好将明文分成块并分别加密。

    【讨论】:

      【解决方案2】:

      AES GCM 使用称为 GCTR 的模式。它类似于 CTR,但在 specific way 中定义。

      当 IV 不是 12 字节时,首先对其进行散列得到 12 字节 IV(GHASH 函数指定为与 Galois 字段的单次乘法,与 MAC 使用的相同)。

      然后将 12 字节的 IV 与 4 字节的计数器(从 1 开始)串联,得到 16 字节的 CTR 块。

      因此,将 IV 视为 BigInteger 并像您在示例中那样增加它绝对行不通。

      【讨论】:

        猜你喜欢
        • 2017-10-15
        • 1970-01-01
        • 2019-03-12
        • 2012-11-05
        • 1970-01-01
        • 2021-09-24
        • 2020-03-20
        • 2020-07-03
        • 2015-03-27
        相关资源
        最近更新 更多