Java GPG解密关于:encrypted message contains a signed message - not literal data.的解决

今天决定写一篇关于GPG加密解密的文章吧,因为最近自己负责的一个项目有用到,客户将文件使用公钥加密后发给我们,我们需要使用对应的私钥进行解密。但是对于GPG的话我也是首次接触,于是参考网上大佬们的博客,我自己在这里做个记录。而让我感到比较有成就感的是,通过查找了一整天的资料,也解决了当解密后的文件包含签名信息时,网上大多数JAVA工具类抛出的encrypted message contains a signed message - not literal data.异常信息

1 关于GPG

首先说到GPG我们则得先了解什么是PGP,因为GPG是作为PGP的替代品出现的。PGP广泛运用与电子邮件,并且多年来是为电子邮件进行加密的标准。但是有点不太好的则是,PGP是个商业软件,不能自由使用,因为这涉及到产权的问题。所以自由软件基金会决定,开发一个PGP的替代品,取名为GnuPG。这就是GPG的由来。其功能与GPG十分相似,但是并不包含PGP的专利算法。

2 软件安装

这个的话我主要是用于测试使用的,所以用到的软件是基于windows环境的,使用的是gpg4win-3.1.13,个人觉得非常好用,因为全界面操作,简单快捷,并且公钥私钥的导出也方便。
在这里插入图片描述
关于这个软件的使用我就不介绍了,有需要的自己百度就可以了。

3 Java代码使用

这里的话与网上大多数工具类不同的是,当对方使用签名加密时,如果我们网上的工具类对文件进行解密时将会报encrypted message contains a signed message - not literal data.的异常。
因为网上的工具类对于包含签名信息的数据文件处理如下:当解密出来的数据文件包含签名时,将会直接抛出异常。

在这里插入图片描述

而可能我们急切的需要将文件进行解密,而忽略掉签名信息。则我们可以判断解密出来的内容包含签名信息时,再去签名信息的下一层内容。如果有多层则可以层层递进的去获取内容,直到获取到数据信息为止,正常情况下,签名信息下一层则为数据信息了,下面就直接上经过我稍微改造过的工具类。有兴趣的也可以看看PGPOnePassSignatureList这个签名信息类的源码。

工具类

package com.kingyea.exchange.gdbyjwanalysis.utils;

import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.*;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.jcajce.*;

import java.io.*;
import java.security.GeneralSecurityException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Iterator;

/**
 * pgp 加解密
 */
@Slf4j
public class PgpUtils {

    private static PgpUtils INSTANCE = null;

    public static PgpUtils getInstance() {

        if (INSTANCE == null) INSTANCE = new PgpUtils();
        return INSTANCE;
    }

    private PgpUtils() {
    }


    public PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
        in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
        PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in);

        //
        // we just loop through the collection till we find a key suitable for encryption, in the real
        // world you would probably want to be a bit smarter about this.
        //
        PGPPublicKey key = null;

        //
        // iterate through the key rings.
        //
        Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings();

        while (key == null && rIt.hasNext()) {
            PGPPublicKeyRing kRing = rIt.next();
            Iterator<PGPPublicKey> kIt = kRing.getPublicKeys();
            while (key == null && kIt.hasNext()) {
                PGPPublicKey k = kIt.next();

                if (k.isEncryptionKey()) {
                    key = k;
                }
            }
        }

        if (key == null) {
            throw new IllegalArgumentException("Can't find encryption key in key ring.");
        }

        return key;
    }

    /**
     * Load a secret key ring collection from keyIn and find the secret key corresponding to
     * keyID if it exists.
     *
     * @param keyIn input stream representing a key ring collection.
     * @param keyID keyID we want.
     * @param pass  passphrase to decrypt secret key with.
     * @return
     * @throws IOException
     * @throws PGPException
     * @throws NoSuchProviderException
     */
    private PGPPrivateKey findSecretKey(InputStream keyIn, long keyID, char[] pass) throws IOException, PGPException, NoSuchProviderException {
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(org.bouncycastle.openpgp.PGPUtil.getDecoderStream(keyIn));

        PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID);

        if (pgpSecKey == null) {
            return null;
        }

        PBESecretKeyDecryptor a = new JcePBESecretKeyDecryptorBuilder(new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(pass);

        return pgpSecKey.extractPrivateKey(a);
    }

    /**
     * decrypt the passed in message stream
     */
    public String decryptFile(InputStream in, InputStream keyIn) throws Exception {
        return decryptFile(in, keyIn, "123456".toCharArray());
    }

    public String decryptFile(InputStream in, InputStream keyIn, char[] passwd) throws Exception {
        String result = null;
        Security.addProvider(new BouncyCastleProvider());
        in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
        PGPObjectFactory pgpF = new PGPObjectFactory(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
        //
        Iterator<PGPPublicKeyEncryptedData> it = enc.getEncryptedDataObjects();
        PGPPrivateKey sKey = null;
        PGPPublicKeyEncryptedData pbe = null;

        while (sKey == null && it.hasNext()) {
            pbe = it.next();
            sKey = findSecretKey(keyIn, pbe.getKeyID(), passwd);
        }

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

        PublicKeyDataDecryptorFactory b = new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").setContentProvider("BC").build(sKey);

        InputStream clear = pbe.getDataStream(b);

        PGPObjectFactory plainFact = new PGPObjectFactory(clear);

        Object message = plainFact.nextObject();

        // if (message instanceof PGPCompressedData) {
        PGPCompressedData cData = (PGPCompressedData) message;
        PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream());
        message = pgpFact.nextObject();
        // }

        // 这里是没带签名的逻辑
        if (message instanceof PGPLiteralData) {
            PGPLiteralData ld = (PGPLiteralData) message;
            InputStream unc = ld.getInputStream();

            StringBuilder sb = new StringBuilder();
            String line;

            BufferedReader br = new BufferedReader(new InputStreamReader(unc));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();
        } else if (message instanceof PGPOnePassSignatureList) { //签名逻辑再解密
            // check signature and extract message
            PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) message;
            PGPOnePassSignature ops = p1.get(0);
            PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
            InputStream dIn = p2.getInputStream();
            StringBuilder sb = new StringBuilder();
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(dIn));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();
        } else {

            throw new PGPException("Message is not a simple encrypted file - type unknown.");
        }

        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
                throw new PGPException("Message failed integrity check");
            }
        }
        return result;
    }


    public void encryptFile(OutputStream out, String fileName, PGPPublicKey encKey, boolean armor,
                            boolean withIntegrityCheck) throws IOException, NoSuchProviderException, PGPException {
        Security.addProvider(new BouncyCastleProvider());

        if (armor) {
            out = new ArmoredOutputStream(out);
        }

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);

        org.bouncycastle.openpgp.PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY, new File(fileName));

        comData.close();

        JcePGPDataEncryptorBuilder c = new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC");

        PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(c);

        JcePublicKeyKeyEncryptionMethodGenerator d = new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider(new BouncyCastleProvider()).setSecureRandom(new SecureRandom());

        cPk.addMethod(d);

        byte[] bytes = bOut.toByteArray();

        OutputStream cOut = cPk.open(out, bytes.length);

        cOut.write(bytes);

        cOut.close();

        out.close();
    }


    private byte[] inputStreamToByteArray(InputStream is) throws IOException {

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int nRead;
        byte[] data = new byte[1024];

        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

        buffer.flush();

        return buffer.toByteArray();
    }


    /**
     * verify the signature in in against the file fileName.
     */
    private boolean verifySignature(String fileName, byte[] b, InputStream keyIn) throws
            GeneralSecurityException, IOException, PGPException {
        //in = PGPUtil.getDecoderStream(in);

        PGPObjectFactory pgpFact = new PGPObjectFactory(b);
        PGPSignatureList p3 = null;

        Object o = pgpFact.nextObject();
        if (o instanceof PGPCompressedData) {
            PGPCompressedData c1 = (PGPCompressedData) o;

            pgpFact = new PGPObjectFactory(c1.getDataStream());

            p3 = (PGPSignatureList) pgpFact.nextObject();
        } else {
            p3 = (PGPSignatureList) o;
        }

        PGPPublicKeyRingCollection pgpPubRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn));


        InputStream dIn = new BufferedInputStream(new FileInputStream(fileName));

        PGPSignature sig = p3.get(0);
        PGPPublicKey key = pgpPubRingCollection.getPublicKey(sig.getKeyID());


        sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider(new BouncyCastleProvider()), key);

        int ch;
        while ((ch = dIn.read()) >= 0) {
            sig.update((byte) ch);
        }

        dIn.close();

        if (sig.verify()) {
            return true;
        } else {
            return false;
        }
    }

    private PGPSecretKey readSecretKey(InputStream input) throws IOException, PGPException {
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input));

        //
        // we just loop through the collection till we find a key suitable for encryption, in the real
        // world you would probably want to be a bit smarter about this.
        //

        Iterator keyRingIter = pgpSec.getKeyRings();
        while (keyRingIter.hasNext()) {
            PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();

            Iterator keyIter = keyRing.getSecretKeys();
            while (keyIter.hasNext()) {
                PGPSecretKey key = (PGPSecretKey) keyIter.next();

                if (key.isSigningKey()) {
                    return key;
                }
            }
        }

        throw new IllegalArgumentException("Can't find signing key in key ring.");
    }

    private byte[] createSignature(String fileName, InputStream keyIn, char[] pass, boolean armor) throws
            GeneralSecurityException, IOException, PGPException {


        PGPSecretKey pgpSecKey = readSecretKey(keyIn);
        PGPPrivateKey pgpPrivKey = pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider(new BouncyCastleProvider()).build(pass));
        PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSecKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(new BouncyCastleProvider()));


        sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ArmoredOutputStream aOut = new ArmoredOutputStream(byteOut);


        BCPGOutputStream bOut = new BCPGOutputStream(byteOut);

        InputStream fIn = new BufferedInputStream(new FileInputStream(fileName));

        int ch;
        while ((ch = fIn.read()) >= 0) {
            sGen.update((byte) ch);

        }

        aOut.endClearText();

        fIn.close();

        sGen.generate().encode(bOut);

        if (armor) {
            aOut.close();
        }

        return byteOut.toByteArray();
    }


    /**
     * 生成签名文件
     *
     * @param filePath       签名文件路径
     * @param privateKeyPath 私钥路径
     * @param outFilePath    输出证书路径
     *                       证书名称必须与签名文件名称一样 多后缀: .asc
     *                       比如: 签名文件为:di.ova   那么生成的证书必须为: di.ova.asc
     * @param passWord       证书密码
     * @return 证书字节数组
     */
    public static byte[] signatureCreate(String filePath, String privateKeyPath, String outFilePath, String
            passWord) {

        try {
            FileInputStream privKeyIn = new FileInputStream(privateKeyPath);
            FileOutputStream signatureOut = new FileOutputStream(outFilePath);
            byte[] sig = PgpUtils.getInstance().createSignature(filePath, privKeyIn, passWord.toCharArray(), true);
            signatureOut.write(sig);
            signatureOut.flush();
            signatureOut.close();
            privKeyIn.close();
            return sig;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 签名验证
     *
     * @param filePath      被签名的文件路径
     * @param publicKeyPath 公钥路径
     * @param signFilePath  签名文件路径
     * @return 是否通过
     */
    public static boolean verifySignature(String filePath, String publicKeyPath, String signFilePath) {
        try {
            FileInputStream pubKeyIs = new FileInputStream(publicKeyPath);
            FileInputStream signFile = new FileInputStream(signFilePath);
            byte[] signFileBytes = new byte[signFile.available()];
            signFile.read(signFileBytes);
            final boolean verifyResult = PgpUtils.getInstance().verifySignature(filePath, signFileBytes, pubKeyIs);
            signFile.close();
            pubKeyIs.close();
            return verifyResult;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

测试类

package com.kingyea.exchange.gdbyjwanalysis.utils;

import java.io.File;
import java.io.FileInputStream;


public class PgpTest {
    public static void main(String[] args) throws Exception {
        PgpUtils pgpUtils = PgpUtils.getInstance();
        // gpg签名加密:入参第一个为加密的文件 第二个为密钥
        String result = pgpUtils.decryptFile(new FileInputStream(new File("D:\\root\\ex_dishui_03v2_yhz_fmg_20201102161546_01.xml.gpg")),
                new FileInputStream(new File("D:\\root\\tuxusheng_0xEF9440A4_SECRET.asc")));

        System.out.println(result);

        // Map<String, Object> map = XmlMapUtils.xmlStr2map(result, false);
        // //        System.out.println(map.size());
        //
        // //        for (Map.Entry<String, Object> e : map.entrySet()) {
        // //            System.out.println(e.getKey());
        // //            System.out.println(e.getValue().getClass());
        // //        }
        //
        // List<Map<String, String>> list = (List<Map<String, String>>) map.get("Row");
        // for (Map m : list) {
        //     System.out.println(m);
        // }
    }
}

4 总结

我每次遇到问题时,我的第一个想法就是,我就是个普通的开发人员,我遇到的问题大佬们肯定也都遇到过,即使没有那么肯定也有大佬去研究过,所以当遇到问题时,我一般不会紧张,而是学会去分析以及如何通过关键字去网上进行搜索,哪怕百度找不到,那我们就换谷歌。最后也希望自己越来越强。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值