java - 使用 Java 中的 BouncyCaSTLe PGP 实用程序进行增量加密

标签 java encryption bouncycastle gnupg pgp

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

  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();
    }

我有一个小的原型(prototype)测试类来使用该方法,如下所示:

            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 解密它们?

最佳答案

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

关于java - 使用 Java 中的 BouncyCaSTLe PGP 实用程序进行增量加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46592290/

相关文章:

java - 从 ReSTLet 请求中读出自定义 header

python - 将二进制数据加密为二进制数据并解密

Javascript 到 Java AES

javascript - AES CBC : JavaScript/CryptoJS Encrypt -> Golang Decrypt

java - BouncycaSTLeProvider 抛出 java.lang.NoSuchFieldError : id_hmacWithSHA3_224

c# - Bouncy CaSTLe C# 中的 PBKDF2

java - Java中如何获取在线音量

java - wait() 调用时出现 IllegalMonitorStateException

java - 立即停止线程

c# - 带有 P-256 的 ECDH 使用静态 key 生成共享 key bouncy caSTLe c#