用Java刷新PGP加密的Bouncy Castle OutputStream [英] Flush PGP Encrypted Bouncy Castle OutputStream in Java

查看:123
本文介绍了用Java刷新PGP加密的Bouncy Castle OutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个概念证明Java应用程序,该应用程序从PGP加密的文件中读取一系列用换行符分隔的请求,处理这些请求,然后将响应写入到另一个PGP加密的文件中,并在刷新后

I'm working on a proof of concept Java application that reads a series of newline-separated requests from a PGP-encrypted file, processes those requests, and then writes the responses to another PGP-encrypted file, with a flush after each response write.

我已经成功地将Bouncy Castle 1.5与我的应用程序集成在一起,但我似乎无法在命令中刷新输出:

I've successfully integrated Bouncy Castle 1.5 with my application with the exception that I cannot seem to flush the output on command:

private ArmoredOutputStream armoredOut = null;
private OutputStream compressedOut = null;
private OutputStream encryptedOut = null;

public OutputStream encryptStream(OutputStream outputStream){
    OutputStream literalOut = null;
    try{
        armoredOut = new ArmoredOutputStream(outputStream);
        BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.AES_256);
        dataEncryptor.setSecureRandom(new SecureRandom());
        PGPEncryptedDataGenerator encryptGen = new PGPEncryptedDataGenerator(dataEncryptor);

        PGPPublicKey publicKey = null;
        InputStream publicKeyStream = null;
        try{
            publicKeyStream = this.getClass().getClassLoader().getResourceAsStream(keyName);
            publicKey = getEncryptionKey(publicKeyStream);
        }
        finally{
            if(publicKeyStream != null){
                publicKeyStream.close();
            }
        }
        if(publicKey == null){
            throw new IllegalArgumentException("Couldn't obtain public key.");
        }

        encryptGen.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));

        encryptedOut = encryptGen.open(armoredOut, new byte[bufferSize]);

        PGPCompressedDataGenerator compressGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
        compressedOut = compressGen.open(encryptedOut);

        PGPLiteralDataGenerator literalGen = new PGPLiteralDataGenerator();
        literalOut = literalGen.open(compressedOut, PGPLiteralDataGenerator.UTF8, "Response", new Date(), new byte[bufferSize]);
    }
    catch(PGPException e){
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    catch(IOException e){
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    return literalOut;
}

当我显式调用flush()时,返回的OutputStream不刷新。只有在每个compressedOut,encryptedOut和armoredOut OutputStream上调用close()方法时,它们才真正被刷新。

The returned OutputStream does not flush when I explicitly call flush(). Only when the close() method is called on each of the compressedOut, encryptedOut, and armoredOut OutputStreams are they actually flushed.

我试图修改Bouncy Castle源代码代码,但我所做的一切都会导致某种格式错误或损坏的PGP消息,无法解密。我也尝试过修改缓冲区的大小,以使其更小,更大以及单个请求的确切大小,但这没有用。

I've tried to modify the Bouncy Castle source code, but everything I do leads to some sort of malformed or corrupted PGP message that cannot be decrypted. I've also tried to modify the buffer size to make it smaller, larger, and the exact size of a single request, but this didn't work.

有人吗?对于如何使用Bouncy Castle手动刷新加密的OutputStream有任何建议?

Does anyone have any suggestions on how to manually flush an encrypted OutputStream with Bouncy Castle?

推荐答案

我在BC中也遇到了同样的问题。查看 ArmoredOutputStream 类。 刷新为空,并且关闭并不称其为基础输出流关闭。这意味着,如果您使用的是 ArmoredOutputStream ArmoredInputStream ,则必须关闭 ArmoredOutputStream 本身和基础 Outputstream 。同花顺!

I had the same problem with BC. Look into the ArmoredOutputStream class. Flush is empty and close doesn't call it's underlying outputstreams close. That means if you're working with ArmoredOutputStream or ArmoredInputStream you have to close ArmoredOutputStream itself AND the underlying Outputstream. The same for flush!

这篇关于用Java刷新PGP加密的Bouncy Castle OutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆