java pgp加密_如何解密PGP加密文件(由两个PGP密钥加密.Key1具有公钥,私钥.Key2只有公钥)通过JAVA API...
我在PGP桌面有两个PGP密钥。
Key1 - 在我的PGP桌面中创建。其中包含公钥和私钥
Key2 - 由我的客户创建并与我们共享公钥。我只在我的PGP桌面中添加了key2公钥。
现在我使用上面的两个密钥加密文件。我可以使用PGP Desktop成功解密文件。
但是我无法使用Java API解密文件。
public static void decryptFile(File input, File key, char[] passwd, File destDir) throws Exception {
File destFile = null;
InputStream keyIn = new FileInputStream(key);
Security.addProvider(new BouncyCastleProvider());
InputStream is = new FileInputStream(input);
InputStream in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(is);
try {
PGPObjectFactory pgpF = new PGPObjectFactory(in);
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
Iterator encDataObjects = enc.getEncryptedDataObjects();
PGPPublicKeyEncryptedData encryptedData = null;
List keyIds = new LinkedList();
while (encDataObjects.hasNext()) {
encryptedData = (PGPPublicKeyEncryptedData) encDataObjects.next();
long decipheringKeyID = encryptedData.getKeyID();
System.out.println("****** Key ID ****** "+decipheringKeyID);
keyIds.add(decipheringKeyID);
}
PGPSecretKey decipheringKey = readSecretKey(keyIn, keyIds);
if (decipheringKey == null) {
throw new IllegalArgumentException("Secret key for message not found.");
}
PGPPrivateKey decryptionKey = findPrivateKey(decipheringKey, passwd);
if (decryptionKey == null) {
throw new IllegalArgumentException("Private key for message not found.");
}
PublicKeyDataDecryptorFactory dataDecryptorFactory = new BcPublicKeyDataDecryptorFactory(decryptionKey);
InputStream clear = encryptedData.getDataStream(dataDecryptorFactory);
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 PGPOnePassSignatureList) {
// TODO: Verify file
message = plainFact.nextObject();
}
if (message instanceof PGPLiteralData) {
PGPLiteralData ld = (PGPLiteralData) message;
InputStream unc = ld.getInputStream();
int ch;
ByteArrayOutputStream out = new ByteArrayOutputStream();
if(ld.getFileName().toString() == null || ld.getFileName().toString().equals(""))
{
String[] fname = input.getAbsolutePath().split("\\\\");
destFile = new File(destDir,fname[fname.length-1].replace(".pgp", ""));
}
else
{
destFile = new File(destDir, ld.getFileName());
}
FileOutputStream fos = new FileOutputStream(destFile);
while ((ch = unc.read()) >= 0) {
out.write(ch);
}
byte[] returnBytes = out.toByteArray();
out.close();
fos.write(returnBytes);
fos.close();
} else {
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
} catch (Exception ex) {
ex.printStackTrace();
throw new Exception(ex.getMessage());
} finally {
if (keyIn != null) {
keyIn.close();
}
if (in != null) {
in.close();
}
if (is != null) {
is.close();
}
}
}我得到以下错误
org.bouncycastle.openpgp.PGPException: exception encrypting session info: unknown block type
at org.bouncycastle.openpgp.operator.bc.BcPublicKeyDataDecryptorFactory.recoverSessionData(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at com.pgp.util.PGPUtils.decryptFile(PGPUtils.java:312)
at com.pgp.util.PGPUtils.main(PGPUtils.java:576)
Caused by: org.bouncycastle.crypto.InvalidCipherTextException: unknown block type
at org.bouncycastle.crypto.encodings.PKCS1Encoding.decodeBlock(Unknown Source)
at org.bouncycastle.crypto.encodings.PKCS1Encoding.processBlock(Unknown Source)
at org.bouncycastle.crypto.BufferedAsymmetricBlockCipher.doFinal(Unknown Source)
... 4 more
Exception in thread "main" java.lang.Exception: exception encrypting session info: unknown block type
at com.pgp.util.PGPUtils.decryptFile(PGPUtils.java:373)
at com.pgp.util.PGPUtils.main(PGPUtils.java:576)
java pgp加密_如何解密PGP加密文件(由两个PGP密钥加密.Key1具有公钥,私钥.Key2只有公钥)通过JAVA API...相关推荐
- java 防钓鱼_验证表单重复提交(防止钓鱼,密码加密,自定义标签,过滤器)...
包含几种加密方法 package com.chinasofti.util.sec; import java.io.IOException; import java.io.UnsupportedEnco ...
- mysql主从同步加密_教你构建MySQL主从结构,实现基于SSL加密的主从同步机制
实验环境RHEL6.4 admin1.tuchao.com 192.168.1.201 主服务器 admin2.tuchao.com 192.168.1.202 从服务器 先在 ...
- 软件工程学完java后干_软件工程学习后的一些体会--------两周
软件工程这门课在印象中就是一门理论课,因为以前没有接触过,想象中的场景是:老师打开ppt,然后照本宣科的开始念经.在第一节课上,作为见面礼,老师开场赠送的一波营养丰富的鸡汤确实很是鲜美,听后信誓旦旦的 ...
- java 类隔离_微服务架构中zuul的两种隔离机制实验
ZuulException REJECTED_SEMAPHORE_EXECUTION 是一个最近在性能测试中经常遇到的异常.查询资料发现是因为zuul默认每个路由直接用信号量做隔离,并且默认值是100 ...
- java计算里程_通过角速度计算里程(对于两轮机器人)?
我需要对机器人进行编程以检测它的位置,但由于我没有外部监视器/摄像头,因此机器人需要在内部计算其位置 . 但无论我做多少研究,我都无法做到这一点 . 因为机器人也需要避开障碍物,所以速度和方向总是在不 ...
- java 浏览器内核_新版 Edge 浏览器或将拥有两个不同的浏览器内核
微软即将发布的新版 Edge 浏览器很可能同时包含 ChromiumBlink 和 MSHTML(又称 Trident)两个浏览器内核,方便浏览器给那些为 IE 设计的网站提供向后兼容性. 据爆料人士 ...
- python能解密java的_实现Java加密,Python解密的RSA非对称加密算法功能
摘要 因为最近业务需要使用到openssl的rsa非对称加密算法,研究了下它的使用方式,但是特殊在于前端分IOS和android两端,所以前端部门要求使用java给他们做一个加密工具包,但是因为服务端 ...
- java python rsa加密_实现Java加密,Python解密的RSA非对称加密算法功能
摘要 因为最近业务需要使用到openssl的rsa非对称加密算法,研究了下它的使用方式,但是特殊在于前端分IOS和android两端,所以前端部门要求使用java给他们做一个加密工具包,但是因为服务端 ...
- 非对称加密 公钥私钥_选择Java加密算法第3部分–公钥/私钥非对称加密
非对称加密 公钥私钥 抽象 这是涵盖Java加密算法的三部分博客系列的第3部分. 该系列涵盖如何实现以下功能: 使用SHA–512散列 使用AES–256的单密钥对称加密 RSA–4096 这第三篇文 ...
最新文章
- 值得推荐的好书——评《亮剑.NET.图解C#开发实战》
- 6-java代码片段
- DevicePass-through及网卡的直接分配在Xen里面的实现
- 在windows上的git bash中安装tree 和 linux tree命令使用
- c++ smart pointer 趣谈
- 使用Math 类 和 Random类 两种方式生成 20 到30之间的随机整数---java基础
- 软件——机器学习与Python,Python3的输出与输入
- yum安装php7.2
- CentOS 6.4下操作kdump执行过程
- eclipse和idea代码通用吗_25个JavaScript代码简写技巧(下篇)
- 通过fork进程爆破canary
- movebase导航
- 数据增强 | 现实应用思考
- ffmpeg转码cpu很高,ffmpeg实现GPU(硬编码)加速转码,解决ffmpeg转码速度慢
- m1 mac屏幕保护程序取消不了怎么办
- java项目 ppt,Java项目报告ppt
- 怎么可以修改pr基本图形中的文字_视频剪辑 | pr的简单教学
- gtx1060 能用catia软件吗_性能炸天!资深IT大叔详测Precision 7730工作站
- 张亚勤寄语哥伦比亚大学2020年毕业生:引领未知时代
- Matlab--优化工具箱