We recently upgraded our BouncyCastle .jar files to use the latest available version, and have been working to implement them into our existing code.
In testing the encryption method, we've found that the file created lacks the "END PGP MESSAGE" end tag. And seems to also lack the trailing encryption lines to mark the signature.
This is our implementation of the signed output writer - are we missing an essential step to signing our file? Or is our implementation wrong?
public static void writeSignedOutputStream(PGPKeyPair keyPair, byte[] data, OutputStream outStream, char dataType, String fileName, boolean withArmor)
throws IOException, NoSuchAlgorithmException, NoSuchProviderException, PGPException, SignatureException
{
try{
registerProvider();
if(withArmor)
outStream = new ArmoredOutputStream(outStream);
PGPPrivateKey privateKey = keyPair.getPrivateKey();
PGPPublicKey publicKey = keyPair.getPublicKey();
// Original signature generator
//PGPSignatureGenerator generator = new PGPSignatureGenerator(publicKey.getAlgorithm(), PGPUtil.SHA1, provider.getName());
PGPSignatureGenerator sigGenerator =
new PGPSignatureGenerator(new BcPGPContentSignerBuilder(publicKey.getAlgorithm(), PGPUtil.SHA1));
// Updating for new version of BouncyCastle
//generator.initSign(PGPSignature.BINARY_DOCUMENT, privateKey);
sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
for(Iterator i = publicKey.getUserIDs(); i.hasNext(); ){
PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator();
subpacketGenerator.setSignerUserID(false, (String)i.next());
sigGenerator.setHashedSubpackets(subpacketGenerator.generate());
}
BCPGOutputStream bcpgStream = new BCPGOutputStream(outStream);
sigGenerator.generateOnePassVersion(true).encode(bcpgStream);
PGPLiteralDataGenerator dataGenerator = new PGPLiteralDataGenerator(false);
OutputStream dataStream = dataGenerator.open(bcpgStream, dataType, fileName, data.length, new Date());
for(int c = 0; c < data.length; c++){
dataStream.write(data[c]);
sigGenerator.update(data[c]);
}
sigGenerator.generate().encode(bcpgStream);
dataStream.close();
dataGenerator.close();
bcpgStream.close();
}catch(PGPException pe){
//Exception catching
}catch(Exception e){
//Exception Catching
}finally{}
}
Here's what the output file looks like [with encrypted data censored] to give an illustration of the issue - I'm expecting to see "--END PGP MESSAGE--" at the bottom of this message, but it is simply not there.