I have a file encrypted using GPG that I would like to extract the session key from so I can decrypt the session key separately. I'm using Bouncy Castle to extract the session key using the following code:
private static void outputSessionKey(String path) throws FileNotFoundException, IOException {
BCPGInputStream input = new BCPGInputStream(PGPUtil.getDecoderStream(new FileInputStream(path)));
Packet packet;
while((packet = input.readPacket()) != null) {
if (packet instanceof PublicKeyEncSessionPacket) {
PublicKeyEncSessionPacket encPacket = (PublicKeyEncSessionPacket) packet;
byte[] encKey = encPacket.getEncSessionKey()[0];
FileOutputStream output = new FileOutputStream("session_key_enc.bin");
output.write(encKey);
output.close();
}
}
input.close();
}
I then decrypt the session key using the following OpenSSL command:
openssl rsautl -decrypt -in session_key_enc.bin -out session_key_dec.bin -inkey private.pem -raw
I would expect the raw output to now be the decrypted session key but I am unable to use it to decrypt the original file using --override-session-key.
Does anyone have an idea about what could be going wrong with this setup?