相关疑难解决方法(0)

如何使用bouncycastle pgp加密字符串/流而不启动文件

我需要使用bouncycastle提供程序使用pgp加密流.我能找到的所有示例都是关于获取纯文本文件并加密,但是我没有文件,重要的是纯文本永远不会写入磁盘.

我见过的大多数方法都在使用

想要传入明文的PGPUtil.writeFileToLiteralData.我宁愿传递一个byte []或一个inputStream.

有人能指出我的一个例子

  • 从string/byte []/inputstream开始
  • 将所述字符串/ byte []加密到我可以写入文件的outputStrem
  • 从inputStream解密

万一其他人偶然发现并想要完整的解决方案

package com.common.security.pgp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.Date;
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.PGPLiteralDataGenerator;
import org.bouncycastle.openpgp.PGPObjectFactory;
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.PGPSecretKeyRingCollection;
import org.bouncycastle.openpgp.PGPUtil;

/**
 * …
Run Code Online (Sandbox Code Playgroud)

java bouncycastle pgp

39
推荐指数
1
解决办法
4万
查看次数

标签 统计

bouncycastle ×1

java ×1

pgp ×1