JAVA实现PGP/GPG加解密加验签

一、引言

上一篇博客中介绍了GPG的基本使用,在原来的项目中我们对接gpg加密的时候,采用的方式,利用java代码执行shell命令行,但是在新对接的项目中,这种方式对方利用 命令行能解密,但是利用他们的代码不能解密,原因是他们代码生成并不是pgp后缀 的加密文件, 而是pgp格式的加密文件。所以小编也利用java生成一个加密文件,这样双方可以愉快的加解密了。

二、代码实现

2.1 maven 依赖

    <dependencies>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpg-jdk15on</artifactId>
            <version>1.50</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.50</version>
        </dependency>
    </dependencies>

2.2 工具类代码



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

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.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.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator;

/**
 * pgp 加解密
 */
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
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
PGP(Pretty Good Privacy)是一种加密和名数据的标准协议,Java可以通过Bouncy Castle库实现PGP加密解密,具体步骤如下: 1. 首先需要导入Bouncy Castle库,可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpg-jdk15on</artifactId> <version>1.68</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpkix-jdk15on</artifactId> <version>1.68</version> </dependency> ``` 2. 生成PGP密钥对(公钥和私钥)。 3. 使用公钥加密明文数据。 ``` PGPPublicKey publicKey = ... // 获取公钥 byte[] plainText = ... // 明文数据 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC")); encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider("BC")); OutputStream encryptedOutputStream = encryptedDataGenerator.open(outputStream, new byte[1024]); encryptedOutputStream.write(plainText); encryptedOutputStream.close(); byte[] encryptedData = outputStream.toByteArray(); ``` 4. 使用私钥解密加密后的数据。 ``` PGPSecretKey secretKey = ... // 获取私钥 char[] passPhrase = ... // 私钥的口令 InputStream inputStream = new ByteArrayInputStream(encryptedData); PGPObjectFactory objectFactory = new JcaPGPObjectFactory(inputStream); PGPEncryptedDataList encryptedDataList = (PGPEncryptedDataList) objectFactory.nextObject(); Iterator<PGPPublicKeyEncryptedData> it = encryptedDataList.getEncryptedDataObjects(); PGPPublicKeyEncryptedData encryptedData = it.next(); InputStream decryptedInputStream = encryptedData.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(secretKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(passPhrase)))); byte[] decryptedData = IOUtils.toByteArray(decryptedInputStream); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

g-Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值