2 u010644448 u010644448 于 2015.05.29 14:59 提问

java关于pgp解密的问题,急。。。。 38C

之前测试是使用 pgpe -r minzheng jm.txt -o jm1.txt -a 加密方式加密的,可以直接解密
,但是去银行联调后发现银行使用的是pgpe –r AAA –s –u BBB FILE1 –o FILE2 方式加密的,然后就不能解密了。。。纠结。。。我是找了一段代码用代码解密的,

正常情况下不会进去下面这个判断
} else if (message instanceof PGPOnePassSignatureList) {
throw new PGPException(
"encrypted message contains a signed message - not literal data.");
}

以下是代码

package com.hunger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Iterator;

import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.jcajce.JcaPGPObjectFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;
import org.bouncycastle.util.io.Streams;

public class PgpOperation {

public static void decryptFile(String inputFileName, String keyFileName,
        char[] passwd, String defaultFileName) throws IOException,
        NoSuchProviderException {
    Security.addProvider(new BouncyCastleProvider());
    InputStream in = new BufferedInputStream(new FileInputStream(
            inputFileName));
    InputStream keyIn = new BufferedInputStream(new FileInputStream(
            keyFileName));
    decryptFile(in, keyIn, passwd, defaultFileName);
    keyIn.close();
    in.close();
}

/**
 * decrypt the passed in message stream
 */
private static void decryptFile(InputStream in, InputStream keyIn,
        char[] passwd, String defaultFileName) throws IOException,
        NoSuchProviderException {
    in = PGPUtil.getDecoderStream(in);

    try {
        JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
        PGPEncryptedDataList enc;

        Object o = pgpF.nextObject();
        //
        // the first object might be a PGP marker packet.
        //
        if (o instanceof PGPEncryptedDataList) {
            enc = (PGPEncryptedDataList) o;
        } else {
            enc = (PGPEncryptedDataList) pgpF.nextObject();
        }

        //
        // find the secret key
        //
        @SuppressWarnings("rawtypes")
        Iterator it = enc.getEncryptedDataObjects();
        PGPPrivateKey sKey = null;
        PGPPublicKeyEncryptedData pbe = null;
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(
                PGPUtil.getDecoderStream(keyIn),
                new JcaKeyFingerprintCalculator());

        while (sKey == null && it.hasNext()) {
            pbe = (PGPPublicKeyEncryptedData) it.next();

            sKey = PGPExampleUtil.findSecretKey(pgpSec, pbe.getKeyID(),
                    passwd);
        }

        if (sKey == null) {
            throw new IllegalArgumentException(
                    "secret key for message not found.");
        }

        InputStream clear = pbe
                .getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder()
                        .setProvider("BC").build(sKey));

        JcaPGPObjectFactory plainFact = new JcaPGPObjectFactory(clear);

        PGPCompressedData cData = (PGPCompressedData) plainFact
                .nextObject();

        InputStream compressedStream = new BufferedInputStream(
                cData.getDataStream());
        JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(
                compressedStream);

        Object message = pgpFact.nextObject();

        if (message instanceof PGPLiteralData) {
            PGPLiteralData ld = (PGPLiteralData) message;

            String outFileName = ld.getFileName();
            // if (outFileName.length() == 0)
            // {
            outFileName = defaultFileName;
            // }

            InputStream unc = ld.getInputStream();
            OutputStream fOut = new BufferedOutputStream(
                    new FileOutputStream(outFileName));

            Streams.pipeAll(unc, fOut);

            fOut.close();
        } else if (message instanceof PGPOnePassSignatureList) {
            throw new PGPException(
                    "encrypted message contains a signed message - not literal data.");
        } else {
            throw new PGPException(
                    "message is not a simple encrypted file - type unknown.");
        }

        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
                System.err.println("message failed integrity check");
            } else {
                System.err.println("message integrity check passed");
            }
        } else {
            System.err.println("no message integrity check");
        }
    } catch (PGPException e) {
        System.err.println(e);
        if (e.getUnderlyingException() != null) {
            e.getUnderlyingException().printStackTrace();
        }
    }
}

public static void encryptFile(String outputFileName, String inputFileName,
        String encKeyFileName, boolean armor, boolean withIntegrityCheck)
        throws IOException, NoSuchProviderException, PGPException {
    Security.addProvider(new BouncyCastleProvider());
    OutputStream out = new BufferedOutputStream(new FileOutputStream(
            outputFileName));
    PGPPublicKey encKey = PGPExampleUtil.readPublicKey(encKeyFileName);
    encryptFile(out, inputFileName, encKey, armor, withIntegrityCheck);
    out.close();
}

private static void encryptFile(OutputStream out, String fileName,
        PGPPublicKey encKey, boolean armor, boolean withIntegrityCheck)
        throws IOException, NoSuchProviderException {
    if (armor) {
        out = new ArmoredOutputStream(out);
    }

    try {
        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5)
                        .setWithIntegrityPacket(withIntegrityCheck)
                        .setSecureRandom(new SecureRandom())
                        .setProvider("BC"));

        cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey)
                .setProvider("BC"));

        OutputStream cOut = cPk.open(out, new byte[1 << 16]);

        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
                PGPCompressedData.ZIP);

        PGPUtil.writeFileToLiteralData(comData.open(cOut),
                PGPLiteralData.BINARY, new File(fileName),
                new byte[1 << 16]);

        comData.close();

        cOut.close();

        if (armor) {
            out.close();
        }
    } catch (PGPException e) {
        System.err.println(e);
        if (e.getUnderlyingException() != null) {
            e.getUnderlyingException().printStackTrace();
        }
    }
}

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    if (args.length == 0) {
        System.err
                .println("usage: KeyBasedLargeFileProcessor -e|-d [-a|ai] file [secretKeyFile passPhrase|pubKeyFile]");
        return;
    }

    if (args[0].equals("-e")) {
        if (args[1].equals("-a") || args[1].equals("-ai")
                || args[1].equals("-ia")) {
            encryptFile(args[2] + ".asc", args[2], args[3], true,
                    (args[1].indexOf('i') > 0));
        } else if (args[1].equals("-i")) {
            encryptFile(args[2] + ".bpg", args[2], args[3], false, true);
        } else {
            encryptFile(args[1] + ".bpg", args[1], args[2], false, false);
        }
    } else if (args[0].equals("-d")) {
        decryptFile(args[1], args[2], args[3].toCharArray(), args[4]);
    } else {
        System.err
                .println("usage: KeyBasedLargeFileProcessor -d|-e [-a|ai] file [secretKeyFile passPhrase|pubKeyFile]");
    }
}

}

5个回答

u010644448
u010644448   2015.05.29 20:01

就这样沉了吗?????????

testcs_dn
testcs_dn   Rxr 2015.05.31 06:59

不能解密了,是解密的结果不符合预期?还是出异常了?

xzx4959
xzx4959   2015.07.09 15:28

您好,我想问一下你这个问题解决了没有,最近我也是出现了这个问题,比较着急。能否说一下你的解决思路?非常感谢!

xzx4959
xzx4959   2015.07.09 15:28

能否留下你的邮箱,邮件沟通一下zongxin.xzx@alibaba-inc.com

qq_35460175
qq_35460175   2017.11.14 13:38

Exception in thread "main" java.io.IOException: unknown object in stream: 21
at org.bouncycastle.openpgp.PGPObjectFactory.nextObject(Unknown Source)
at org.bouncycastle.openpgp.PGPSecretKeyRingCollection.(Unknown Source)
这是什么问题呀

Csdn user default icon
上传中...
上传图片
插入图片