【问题标题】:Android log encryption using AES and RSA使用 AES 和 RSA 的 Android 日志加密
【发布时间】:2020-02-11 19:10:31
【问题描述】:

我想从我的应用程序中邮寄加密的日志文件。由于日志可以更大,我使用 AES 加密数据并使用 RSA 加密密钥。由于解密日志需要 AES 密钥,因此我将加密密钥和日志发送到同一个文件中。

问题 1:这是正确的方法吗?如果不是这种情况下最好的方法是什么。下面是相同的代码。

 public static String encrypt(String data) {
    StringBuilder encryptedData = new StringBuilder();
    try {
        // Generate AES key.
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        // The AES key size in number of bits.
        generator.init(256);
        SecretKey secKey = generator.generateKey();

        // Initialize AES Cipher, IV and encrypt string.
        Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        aesCipher.init(Cipher.ENCRYPT_MODE, secKey, new IvParameterSpec(new byte[16]));
        byte[] byteCipherText = aesCipher.doFinal(data.getBytes());
        String encryptedText = Base64.encodeToString(byteCipherText, Base64.DEFAULT);

        // Initialize RSA Cipher and generate public key.
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(Base64.decode(PUBLIC_KEY, Base64.DEFAULT));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey puKey = keyFactory.generatePublic(publicSpec);
        cipher.init(Cipher.PUBLIC_KEY, puKey);

        // Encrypt key and text.
        byte[] encryptedKey = cipher.doFinal(secKey.getEncoded());
        String aesKey = Base64.encodeToString(encryptedKey, Base64.DEFAULT);
        encryptedData.append(aesKey);
        encryptedData.append(encryptedText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedData.toString();
}

【问题讨论】:

  • 如果您想使用 RSA,请使用 RSA-KEM。
  • @kelalaka 使用 RSA-KEM 有什么好处?
  • 专为Key encapsulation Mechanism设计。无需填充。
  • @kelalaka:由于 Android API 不支持 KEM,OP 必须自己实现它。结果可能不如使用已经支持的东西(如 RSA OAEP)安全。
  • @JamesKPolk 不是 Android API,但 Bouncy Castle 有它。此外,在 RSA 密钥生成之后,所有人都需要一个好的随机源和一个 KDF。不像 PKCS#1 v1.5 填充或 OAEP 那样复杂。 OAEP 实际上应该使用 xof 而不是 MFG 进行更新。

标签: android encryption aes rsa


【解决方案1】:

由于解密日志需要 AES 密钥,因此我将加密密钥和日志发送到同一个文件中。

问题 1:这是正确的方法吗?如果不是这种情况下最好的方法是什么。下面是相同的代码。

方法是正确的,我缺少的是身份验证(HMAC,GCM,...)。

有一些标准如何将加密的密钥、内容和身份验证捆绑在一起(例如CMS、PKCS7、OpenPGP、..),但是如果是针对您自己的应用程序,您可以按照自己的方式进行(不要打扰有标准)。

如果你想使用 RSA,请使用 RSA-KEM

好吧,使用 RSA KEM 可以节省一些跳过填充的性能,但如果它对您可行,我会尝试。使用不同的公钥加密相同的密钥材料时也会出现问题。

我会保持简单 - 只需使用正确填充的 RSA 加密。

我建议使用 OAEP 填充 RSA/ECB/OAEPWithSHA-256AndMGF1Padding 而不是 PKCS1Padding(OAEP 被认为更新/更安全)

【讨论】:

  • 出于身份验证目的,我正在考虑使用 GCM 模式,它会隐式执行,对吧?
  • @AjinkyaPatil 确实,GCM 模式可以完成这项工作
  • Gusto, KEM This composition of a KEM and a DEM (data encapsulation mechanism; an authenticated cipher serves as a DEM) provides the standard of IND-CCA2/NM-CCA2—ciphertext indistinguishability and nonmalleability under adaptive chosen-ciphertext attack.this。此外,RSA-KEM 易于使用。
  • @kelalaka 维基百科链接声称:Note that if the same m is used to encapsulate keys for e or more recipients, and the receivers share the same exponent e, but different p, q, and n, then one can recover m via the Chinese remainder theorem. Thus, if key encapsulations for several recipients need to be computed, independent values m should be used.(加上在 java/android 中使用带有默认加密 API 的 KEM 需要额外的调整)
  • 这是通常的 RSA 广播攻击警告。对于 KEM,您生成一个具有新随机值的新密钥。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
相关资源
最近更新 更多