【问题标题】:Incremental Encryption with BouncyCastle PGP Utilities in Java使用 Java 中的 BouncyCastle PGP 实用程序进行增量加密
【发布时间】:2018-03-17 11:11:27
【问题描述】:

我正在尝试编写一个加密文件,该文件将使用 gpg 解密,并且将逐步写入行而不是一个块。我已经在 GnuPG 中生成了密钥,并且正在使用公钥进行加密。这是我用来加密的方法:

  public static byte[] encrypt(byte[] clearData, PGPPublicKey encKey,
            String fileName,boolean withIntegrityCheck, boolean armor)
            throws IOException, PGPException, NoSuchProviderException {
        if (fileName == null) {
            fileName = PGPLiteralData.CONSOLE;
        }

        ByteArrayOutputStream encOut = new ByteArrayOutputStream();

        OutputStream out = encOut;
        if (armor) {
            out = new ArmoredOutputStream(out);
        }

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
                PGPCompressedDataGenerator.ZIP);
        OutputStream cos = comData.open(bOut); // open it with the final
        // destination
        PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();

        // we want to generate compressed data. This might be a user option
        // later,
        // in which case we would pass in bOut.
        OutputStream pOut = lData.open(cos, // the compressed output stream
                PGPLiteralData.BINARY, fileName, // "filename" to store
                clearData.length, // length of clear data
                new Date() // current time
                );
        pOut.write(clearData);

        lData.close();
        comData.close();

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


        cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey));

        byte[] bytes = bOut.toByteArray();

        OutputStream cOut = cPk.open(out, bytes.length);

        cOut.write(bytes); // obtain the actual bytes from the compressed stream

        cOut.close();

        out.close();

        return encOut.toByteArray();
    }

我有一个小型原型测试类来使用这样的方法:

            PGPPublicKey pubKey = PGPEncryptionTools.readPublicKeyFromCol(new FileInputStream(appProp.getKeyFileName()));

        byte[] encryptbytes = PGPEncryptionTools.encrypt("\nthis is some test text".getBytes(), pubKey, null, true, false);
        byte[] encryptbytes2 = PGPEncryptionTools.encrypt("\nmore test text".getBytes(), pubKey, null, true, false);

        FileOutputStream fos = new FileOutputStream("C:/Users/me/workspace/workspace/spring-batch-project/resources/encryptedfile.gpg");
        fos.write(encryptbytes);
        fos.write(encryptbytes2);
        fos.flush();
        fos.close();

所以这会创建encryptedfile.gpg,但是当我去 GnuPG 解密文件时,它可以工作,但它只输出第一行“这是一些测试文本”。

如何修改此代码以加密这两行并让 GnuPG 解密它们?

【问题讨论】:

    标签: java encryption bouncycastle gnupg pgp


    【解决方案1】:

    每次调用 PGPEncryptionTools.encrypt(...) 方法时,您都会生成多个独立的 OpenPGP 消息。要仅输出单个 OpenPGP 消息(GnuPG 也会在一次运行中解密),请将所有纯文本写入单个流(在您的代码中称为 pOut),并且在最终将最后一个字节写入流之前不要关闭它。

    【讨论】:

    • 那么是否有可能,例如,打开这个流并在春季批处理中继续以块的形式写入它,然后关闭流以生成一个加密文件?
    • 是的,完全正确。只要您写入单个流,就会创建单个 OpenPGP 消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2020-09-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多