使用多个RSA公钥进行加密

4

我正在为服务器编写一个模块,用于发送电子邮件。在客户端应用程序中,用户可以添加许多收件人,每个收件人都有自己的公钥。我想使用多个密钥加密附件。例如,如果我添加了3个收件人,则应该使用3个不同的公钥加密附件。我正在使用Bouncy Castle来实现,但它只能用于加密过程中的第一个公钥。我的意思是,只有第一个人可以使用自己的私钥解密,而对于其他人则不起作用。 我为每个密钥添加方法的代码如下:

PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);

for (PGPPublicKey publicKey : publicKeys) {
        encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
}

整个方法看起来像这样:
public File encryptFile(String fileName,
        boolean armor,
        boolean withIntegrityCheck) throws IOException,
        NoSuchProviderException,
        PGPException {
    Security.addProvider(new BouncyCastleProvider());

    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    PGPCompressedDataGenerator comData
            = new PGPCompressedDataGenerator(PGPCompressedData.UNCOMPRESSED);

    PGPUtil.writeFileToLiteralData(comData.open(bOut),
            PGPLiteralData.BINARY,
            new File(fileName));

    comData.close();

    BcPGPDataEncryptorBuilder dataEncryptor
            = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256);

    dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);

    dataEncryptor.setSecureRandom(new SecureRandom());

    PGPEncryptedDataGenerator encryptedDataGenerator
            = new PGPEncryptedDataGenerator(dataEncryptor);

    for (PGPPublicKey publicKey : publicKeys) {
        encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
    }

    byte[] bytes = bOut.toByteArray();

    FileOutputStream localByteArrayOutputStream = new FileOutputStream(fileName);

    Object localObject = localByteArrayOutputStream;

    if (armor) {

        localObject = new ArmoredOutputStream((OutputStream) localObject);

    }

    OutputStream localOutputStream = encryptedDataGenerator.open((OutputStream) localObject,
            bytes.length);

    localOutputStream.write(bytes);

    localOutputStream.close();

    return new File(fileName);
}

有人可以帮助我并告诉我我做错了什么吗?

感谢每一个帮助。

[编辑] 这段代码可以运行,我在加载多个键的方法中遇到了问题。


请参考https://dev59.com/MkXRa4cB1Zd3GeqPohDs,了解更标准的方法。 - jonrsharpe
我明白需要生成对称密钥,使用它来加密文件,然后使用每个公钥加密此密钥,但我不知道如何在bouncycastle中完成此操作。 我需要使其能够使用OpenPGP标准的任何程序进行解密。 - Karol Płoski
1个回答

3

嗯,我一年后也遇到了同样的问题。希望你已经解决了你的问题。我在这里写下我的解决方案,以防其他人有类似的问题。

你的加密代码没有问题。问题可能出现在解密过程中。对于一个加密的数据对象,正确的密钥应该通过使用存储在对象中的密钥 id 来找到。我的解密过程如下所示:

private byte[] decryptWithKey(byte[] bytes, PGPSecretKey secKey, String pass)
        throws PGPException, IOException {
    PBESecretKeyDecryptor keyDec = new JcePBESecretKeyDecryptorBuilder(
            new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build())
            .setProvider("BC").build(pass.toCharArray());
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    PGPPrivateKey privateKey = secKey.extractPrivateKey(keyDec);
    PublicKeyDataDecryptorFactory dec1 =
            new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(privateKey);
    JcaPGPObjectFactory objFact = new JcaPGPObjectFactory(bytes);
    PGPEncryptedDataList encList = (PGPEncryptedDataList) objFact.nextObject();

    PGPPublicKeyEncryptedData encD = null;
    for(Iterator<PGPPublicKeyEncryptedData> it = encList.iterator(); it.hasNext(); ) {
        PGPPublicKeyEncryptedData end = it.next();
        if (secKey.getKeyID() == end.getKeyID()) {
            encD = end;
            break;
        }
    }
    assert encD != null: "Cannot find encrypted data with key: "
            + Long.toHexString(secKey.getKeyID());
    InputStream in = encD.getDataStream(dec1);
    byte[] buf = new byte[BufferSize];
    for (int len; (len = in.read(buf)) >= 0; ) {
        bout.write(buf, 0, len);
    }
    bout.close();
    return bout.toByteArray();
}

关键是for循环,用于找到加密对象的匹配密钥。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接