使用 Java (Groovy) 解密 GPG 文件

发布于 2025-01-11 19:32:07 字数 2115 浏览 2 评论 0原文

我在网上和 stackoverflow 上尝试了很多不同的例子,但没有成功。 没有密码,只有 .gpg 或 .asc pub/sec 文件。

根据离开公司的前项目维护人员提供的 .gpg 文件,我正在使用 Kleopatra 导出 .asc 文件。

我遇到与 BouncyCastle Open PGP - 中的未知对象相同的错误流 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
}

我在所有尝试中得到的是: 迭代器无法获取下一个对象:流中的未知对象:47

我尝试将密钥文件转换为 ANSI 或 ASCII 西欧/OEM850,但没有帮助。

这是使用不同的 bouncycastle 库,例如 name.neuhalfen.projects.crypto.bouncycastle.openpgp 或 org.c02e.jpgpj

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文