java - 我一直在使用 Bouncy Castle API 为字节 [] 进行 PGP 加密和解密,并收到“无效盔甲”错误消息
问题描述
我一直在使用充气城堡 API 在 Java 中进行 PGP 加密/解密工作一段时间,并且我在处理文件方面取得了成功,但我需要使用 inputStream/byte 数组,但我在这样做时遇到了问题。使用以下代码,加密工作没有任何问题。但是对于解密,我遇到了“Invalid Armor”错误,请参阅下面的错误消息。我在谷歌上搜索过,在任何地方都找不到类似的错误。任何帮助将非常感激。让我知道我需要在我的代码中修复什么。我还使用 PGPUtil 类来转换/读取公钥和私钥。
错误:
Encryption Success
Exception in thread "main" java.io.IOException: invalid armor
at org.bouncycastle.bcpg.ArmoredInputStream.readIgnoreSpace(Unknown Source)
at org.bouncycastle.bcpg.ArmoredInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.nextPacketTag(Unknown Source)
at org.bouncycastle.openpgp.PGPObjectFactory.nextObject(Unknown Source)
at com.iice.vega.unity.api.batch.pgp.PGPProcess.decrypt(PGPProcess.java:63)
at com.iice.vega.unity.api.batch.pgp.PGPTest.main(PGPTest.java:64)
这是我的代码:
public class PGPProcess {
private static String publicKeyPath = System.getProperty("user.dir")+"/keys/Public_Key.asc";
private static String privateKeyPath = System.getProperty("user.dir")+"/keys/Private_Key.asc";
private static String password = "";
public static byte[] decrypt(byte[] encrypted)
throws IOException, PGPException, NoSuchProviderException {
Security.addProvider(new BouncyCastleProvider());
InputStream keyIn = new BufferedInputStream(new FileInputStream(stsPrivateKeyPath));
char[] password = "".toCharArray();
InputStream in = new ByteArrayInputStream(encrypted);
in = PGPUtil.getDecoderStream(in);
JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(in);
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
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(), password);
}
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();
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();
InputStream unc = ld.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int ch;
while ((ch = unc.read()) >= 0) {
out.write(ch);
}
byte[] returnBytes = out.toByteArray();
out.close();
return returnBytes;
}
public static byte[] encrypt(byte[] clearData)
throws IOException, PGPException, NoSuchProviderException {
Security.addProvider(new BouncyCastleProvider());
String fileName=null;
boolean withIntegrityCheck =true;
boolean armor = false;
if (fileName == null) {
fileName = PGPLiteralData.CONSOLE;
}
PGPPublicKey encKey = PGPExampleUtil.readPublicKey(stsPublicKeyPath);
ByteArrayOutputStream encOut = new ByteArrayOutputStream();
OutputStream out = encOut;
if (armor) {
out = new ArmoredOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(
PGPCompressedData.ZIP);
OutputStream cos = comData.open(bOut); // open it with the final
// destination
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, // the compressed output stream
PGPLiteralData.BINARY, fileName, // "filename" to store
clearData.length, // length of clear data
new Date() // current time
);
pOut.write(clearData);
lData.close();
comData.close();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(withIntegrityCheck).setSecureRandom(new SecureRandom()).setProvider("BC"));
cPk.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encKey).setProvider("BC"));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cPk.open(out, bytes.length);
cOut.write(bytes); // obtain the actual bytes from the compressed stream
cOut.close();
out.close();
return encOut.toByteArray();
}
}
解决方案
推荐阅读
- angular - 具有多个出口/子问题的角度网格路由
- c# - 如何检查 ViewComponent 视图是否存在?
- powershell - PowerShell 中的 Zen 模式
- python - 安装程序安装失败
- javascript - React Native(Expo)文件对象与 Javascript(jQuery)文件对象 - Blob/文件大小不同(YouTube 数据 API)
- python - Pytest bdd 错误“scenario_wrapper() 缺少 1 个必需的位置参数:'request'”
- zabbix - 如何计算 s3 存储大小并通过电子邮件发送汇总?
- javascript - 如何根据父级宽度创建子级
- android - Android 10以上手机如何开启WiFi热点
- python-3.x - 在多次运行代码并获得随机结果后是否可以找到 random.seed 值