Android 应用程序中 Bouncy Castle 加密中的完整性保护警告

问题描述

参考这个答案link

我收到以下警告:

gpg: WARNING: message was not integrity protected
gpg: Hint: If this message was created before the year 2003 it is
     likely that this message is legitimate.  This is because back
     then integrity protection was not widely used.
gpg: Use the option '--ignore-mdc-error' to decrypt anyway.

完全可以忽略。但我想解决它。通过互联网上的 BCGPG 内容,我将问题缩小到我认为的问题所在。

 PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES).setSecureRandom(new SecureRandom()));

我认为问题出在

BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES)

as SymmetricKeyAlgorithmTags.TRIPLE_DES 是一种相当古老的加密方法。此外,密钥对是非对称的,因此应该在代码中使用其他一些非对称算法。我没有得到实现 AES 和 SHA 等其他方法的正确方法

虽然我不是 100% 确定,但可以提出更多关于解决这个问题的见解。

解决方法

这正是消息所说的。完整性保护描述如下:https://tools.ietf.org/html/rfc4880#section-5.13

我相信您引用的代码没有在 withIntegrityPacket 上设置 BcPGPDataEncryptorBuilder 标志

所以我会改变:

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES).setSecureRandom(new SecureRandom()));

到:

PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES).setSecureRandom(new SecureRandom()).setWithIntegrityPacket(true));

让我知道它是否有效。