JAVA解密PGP文件已测过

1 篇文章 0 订阅

思路

这里就不说怎么弄pgp的公钥私钥如何生成了

 ---  5个路径 思路清晰   
 ---  公钥key publicKey
 ---  私钥key privateKey
 ---  加密好的文件  filePath
 ---  备份文件 upFilePath
 ---  解密后的文件 untieFilePath

 ---  还会有密码 passwrod

标题好吧直接上代码吧不罗嗦了

// 看怎么去执行了 这里做一个定时任务job 
// 去自己扫描文件夹是否有要解密的文件 
// @Scheduled 定时任务的注解 cron 表达式 这里是30秒走一次  不知道可以自己搜一下了解一下
@Scheduled(cron = "30 * * * * ?")
public void fileDecryptTask(){
	try{
		log.info("-------------------开始执行定时任务:PGP文件解密------------------------");
			Collection<File> files = FileUtils.listFiles(new
					File(filePath), new String[]{"pgp"}, true);
			for (File f : files) {
				decryptPgpFile(f.getAbsolutePath(), untieFilePath, upFilePath,
						privateKey,passwrod);
			}
			log.info("-------------------结束执行定时任务:PGP文件解密------------------------");
	}catch (Exception e) {
			log.error("定时解析加密文件出现错误", e);
	}
}


public static String decryptPgpFile(String filePath, String untieFilePath, String upFilePath,
										String privateKeyPath, String password) {
		try {
			File file = new File(filePath);
			String fileName = file.getName();
			String resultPathNew = untieFilePath;
			String name = fileName.substring(0, fileName.indexOf(".")) + ".csv";
			log.info("生成文件NAME---------------:{}",name);
			String resultPath = untieFilePath+ name;
			Security.addProvider(new BouncyCastleProvider());
			KeyBasedLargeFileProcessor.decryptFile(file.getAbsolutePath(),
					privateKeyPath,
					password.toCharArray(),
					resultPathNew,
					name);
			FileUtil.copyFile(file.getAbsolutePath(), upFilePath+ fileName);
			log.info("已复制解析文件到已读文件夹中 [{}]", upFilePath+ fileName);
			FileUtil.delete(file.getAbsolutePath());
			log.info("已删除已读文件");
			return resultPath;
		} catch (Exception e) {
			log.error("解密文件出现异常", e);
		}
		return null;
	}

FileUtil 里的方法

  /**
	 * 删除文件或文件夹
	 * 
	 * @param filePath
	 */
	public static void delete(String filePath) {
		try {
			File file = new File(filePath);
			if (file.exists()){
				if(file.isDirectory()){
					FileUtils.deleteDirectory(file);
				}else{
					file.delete();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	 // 复制
		public static void copyFile(String srcFile,String destFile){
		try {
			if(FileUtil.exist(srcFile)){
				FileUtils.copyFile(new File(srcFile),new File( destFile ));
			}
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

文件下载

KeyBasedLargeFileProcessor 类
点击连接
提取码:tyy7
jar 包
点击连接
提取码:tyy7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐洋QuQ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值