Java - PGP 解密抛出“异常启动解密”

问题描述 投票:0回答:0

我正面临导致 PGPException 的问题:异常开始解密。

[main] INFO com.rxbenefits.service.impl.PGPEncryptionService - Got encrypted stream from input stream
[main] INFO com.rxbenefits.service.impl.PGPEncryptionService - Creating decryptor with provider: BC
[main] INFO com.rxbenefits.service.impl.PGPEncryptionService - Using private key with ID: 3d1dc62715cb3e82

org.bouncycastle.openpgp.PGPException: Exception starting decryption

    at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
    at com.rxbenefits.service.impl.PGPEncryptionService.decryptFile(PGPEncryptionService.java:142)
    at com.rxbenefits.service.impl.PGPEncryptionServiceTest.testDecryptFile(PGPEncryptionServiceTest.java:85)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

代码说我在下面的解密方法中的

InputStream clearStream = publicKeyEncryptedData.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder() .setProvider(new BouncyCastleProvider()) .build(privateKey));
行抛出错误。我有一种感觉,它可能与我的解密方法顶部的
Security.addProvider(new BouncyCastleProvider());
行有关....但我不确定。

这是我第一次使用加密,所以如果可能的话希望得到任何指导。

public static void decryptFile(InputStream in, OutputStream out, InputStream keyIn, char[] passwd) throws IOException, PGPException {
        Security.addProvider(new BouncyCastleProvider());
        try (InputStream encryptedStream = PGPUtil.getDecoderStream(in)) {
            log.info("Got encrypted stream from input stream");

            PGPObjectFactory pgpFactory = new PGPObjectFactory(encryptedStream, new JcaKeyFingerprintCalculator());
            PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) pgpFactory.nextObject();

            if (encryptedDataList == null) {
                log.info("No encrypted data found in input stream");
                throw new PGPException("No encrypted data found in input stream");
            }

            // find the secret key needed to decrypt the message
            PGPSecretKeyRingCollection secretKeyRingCollection = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn), new JcaKeyFingerprintCalculator());
            PGPPrivateKey privateKey = null;
            PGPPublicKeyEncryptedData publicKeyEncryptedData = null;
            for (Object encryptedData : encryptedDataList) {
                if (encryptedData instanceof PGPPublicKeyEncryptedData) {
                    publicKeyEncryptedData = (PGPPublicKeyEncryptedData) encryptedData;
                    PGPSecretKey secretKey = secretKeyRingCollection.getSecretKey(publicKeyEncryptedData.getKeyID());
                    if (secretKey != null) {
                        privateKey = secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passwd));
                        break;
                    } else {
                        log.info("No secret key found for message");
                        throw new IllegalArgumentException("No secret key found for message");
                    }
                }
            }

            if (privateKey == null) {
                log.info("No private key found for message");
                throw new IllegalArgumentException("No private key found for message");
            }
            log.info("Creating decryptor with provider: " + Security.getProvider("BC").getName());
            log.info("Using private key with ID: " + Long.toHexString(publicKeyEncryptedData.getKeyID()));
            // create the decryptor and decrypt the message
            InputStream clearStream = publicKeyEncryptedData.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder()
                    .setProvider(new BouncyCastleProvider())
                    .build(privateKey));
            log.info("Created decryptor and decrypted message");

            PGPObjectFactory plainFactory = new PGPObjectFactory(clearStream, new JcaKeyFingerprintCalculator());
            Object message = plainFactory.nextObject();

            // handle the decrypted message
            if (message instanceof PGPCompressedData) {
                PGPCompressedData compressedData = (PGPCompressedData) message;
                InputStream compressedStream = new BufferedInputStream(compressedData.getDataStream());
                message = new PGPObjectFactory(compressedStream, new JcaKeyFingerprintCalculator()).nextObject();
            }

            if (message instanceof PGPLiteralData) {
                PGPLiteralData literalData = (PGPLiteralData) message;
                try (InputStream literalStream = literalData.getInputStream()) {
                    byte[] buffer = new byte[4096];
                    int bytesRead;
                    while ((bytesRead = literalStream.read(buffer)) != -1) {
                        out.write(buffer, 0, bytesRead);
                    }
                }
                log.info("Decrypted message of type PGPLiteralData");
            } else {
                log.info("Unknown message type: " + message.getClass().getName());
                throw new PGPException("Unknown message type: " + message.getClass().getName());
            }
        }
    }

我正在尝试解密加密文件。我使用工作提供给我的公钥加密文件,并使用他们提供的 sec 密钥和密码解密。

尝试实际解密时似乎失败了。 知道 pub 和 sec 密钥以及密码都是正确的,我想知道我是否没有正确加密文件?这可能吗?当我打开加密文件时,它看起来非常加密。

java encryption bouncycastle pgp openpgp
© www.soinside.com 2019 - 2024. All rights reserved.