0

I've tried many different examples online and from stackoverflow, but to no avail. There is no passphrase, just either .gpg or .asc pub/sec files.

I'm using Kleopatra to export the .asc files, according to the provided .gpg files from the previous project maintainer who left the company.

I'm getting the same error as in BouncyCastle Open PGP - unknown object in stream 47

File decryptFile(File file){

    String newFileName = "/tmp/encrypted-" + file.getName().replace(".gpg", "")

    File newFile = new File(newFileName)

    File tempFile = getFileFromResource('seckeyascii.asc')

    // Just to see that files are read properly, it's fine
    logger.log(tempFile.getText())

    // Attempt 1
    new Decryptor(
            // new Key(getFileFromResource(RESOURCE_PUBRING)),
            // new Key(getFileFromResource(RESOURCE_SECRING))
            new Key(getFileFromResource('pubkeyascii.asc')),
            new Key(getFileFromResource('seckeyascii.asc'))
    ).decrypt(file, newFile)

    // Attempt 2

    File pubringFile = getFileFromResource(RESOURCE_PUBRING)
    File secringFile = getFileFromResource(RESOURCE_SECRING)

    KeyringConfig keyringConfig = KeyringConfigs.withKeyRingsFromFiles(pubringFile, secringFile, KeyringConfigCallbacks.withUnprotectedKeys())

    try {
        FileInputStream cipherTextStream = new FileInputStream(file.getPath())

        FileOutputStream fileOutput = new FileOutputStream(newFileName)
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutput)

        InputStream plaintextStream = BouncyGPG
                .decryptAndVerifyStream()
                .withConfig(keyringConfig)
                .andIgnoreSignatures()
                .fromEncryptedInputStream(cipherTextStream)
    }
    catch (Exception e){
        logger.log("Error decrypting file: ${e.getMessage()}")
        return null
    }
    finally {
        Streams.pipeAll(plaintextStream, bufferedOut)
    }

    return newFile
}

What I'm getting in all tries is: Iterator failed to get next object: unknown object in stream: 47

I tried converting the key files into ANSI or ASCII Western Europe/OEM850, didn't help.

This is using different bouncycastle libs like name.neuhalfen.projects.crypto.bouncycastle.openpgp or org.c02e.jpgpj

Glorious Kale
  • 1,273
  • 15
  • 25
  • This is when I'm testing over AWS Lambda, but locally I just get an output empty file, no errors. – Glorious Kale Mar 11 '22 at 11:56
  • 1
    You could try to use [PGPainless](https://github.com/pgpainless/pgpainless/) instead. Here is an [example how to use it for decryption](https://github.com/pgpainless/pgpainless/blob/master/pgpainless-core/src/test/java/org/pgpainless/example/DecryptOrVerify.java). – vanitasvitae Mar 15 '22 at 13:57
  • Thank you, will try that out and let you know. – Glorious Kale Mar 15 '22 at 14:36
  • Seems PGPainless assumes you have a passphrase @vanita – Glorious Kale Mar 16 '22 at 09:48
  • No it doesn't. There is Passphrase.emptyPassphrase() and for unencrypted keys, you can just use SecretKeyRingProtector.unprotected keys() as decryptor. – vanitasvitae Mar 16 '22 at 16:57

0 Answers0