我正在研究概念证明Java应用程序,它从PGP加密文件读取一系列换行分隔的请求,处理这些请求,然后将响应写入另一个PGP加密后的文件,每次写入响应后都会刷新。刷新PGP加密的Bouncy Castle OutputStream in Java
我已经成功地集成充气城堡1.5与我与我似乎无法刷新的命令输出中的例外适用:
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;
}
返回的OutputStream不刷新的时候我明确调用flush()。只有在每个compressedOut,encryptedOut和armoredOut OutputStreams上调用close()方法时,它们才会被实际刷新。
我试图修改Bouncy Castle源代码,但是我所做的一切都会导致某些格式不正确或损坏的PGP消息无法解密。我也尝试修改缓冲区大小,使其更小,更大,并确保单个请求的大小,但这不起作用。
有没有人有任何建议如何手动冲洗加密的OutputStream与Bouncy城堡?
我不熟悉BC PGP库,但对于许多编码/加密构造,flush没有意义,因此不被支持。例如,加密通常以最终块的填充方案为块定向。并且base64编码类似于面向最终块的填充方案的面向块。没有办法“刷新”和不完整的块,继续编码。 – 2015-02-26 03:24:24