当前位置: 首页 > news >正文

加解密算法+压缩工具

sha256 工具类

package com.fanghui.vota.packages.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;/*** sha256 工具类** @author fanghui on 2017/3/30.*/
public class Sha256Util {private static Logger logger = LoggerFactory.getLogger(Sha256Util.class);private static final int BUFF_SIZE = 1024 * 1024;public static String fileSha256(File file) {FileInputStream in = null;byte[] buffer = new byte[1024];int len;MessageDigest messageDigest = null;try {messageDigest = MessageDigest.getInstance("SHA-256");in = new FileInputStream(file);while ((len = in.read(buffer, 0, 1024)) != -1) {messageDigest.update(buffer, 0, len);}} catch (NoSuchAlgorithmException e) {logger.error("error:" + e);return null;} catch (FileNotFoundException e) {logger.error("error:" + e);return null;} catch (IOException e) {logger.error("error:" + e);return null;} finally {if (in != null) {try {in.close();} catch (IOException e) {logger.error("error:" + e);}}}BigInteger bigInt = new BigInteger(1, messageDigest.digest());return bigInt.toString(16);}/*** TODO 客户端的加密算法** @param deltaFilePath* @return* @author sheng*/public static String getClientSha256(String deltaFilePath) {FileInputStream fis = null;StringBuffer buf = new StringBuffer();try {MessageDigest md = MessageDigest.getInstance("SHA-256");fis = new FileInputStream(deltaFilePath);byte[] buffer = new byte[BUFF_SIZE];int length = -1;if (fis == null || md == null) {return null;}while ((length = fis.read(buffer)) != -1) {md.update(buffer, 0, length);}byte[] bytes = md.digest();if (bytes == null) {return null;}for (int i = 0; i < bytes.length; i++) {String md5s = Integer.toHexString(bytes[i] & 0xff);if (md5s == null || buf == null) {return null;}if (md5s.length() == 1) {buf.append("0");}buf.append(md5s);}return buf.toString();} catch (Exception ex) {logger.error("计算文件hash失败!异常信息:",ex);throw new RuntimeException("calculate file sha256 exception.");} finally {try {if (fis != null) {fis.close();}} catch (IOException ex) {logger.error("计算文件hash失败!异常信息:",ex);}}}}

shell脚本执行类

package com.fanghui.vota.packages.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;/*** TODO shell脚本执行类** @author fanghui*/
public class ShellCommodUtil {private static Logger logger = LoggerFactory.getLogger(ShellCommodUtil.class);/*** 运行shell脚本** @param shell* @return* @throws Exception*/public static boolean runShellBoolean(String shell) throws Exception {logger.info("begin run shell:{}", shell);String[] cmd = {"/bin/sh", "-c", shell};//执行命令Process process = Runtime.getRuntime().exec(cmd);InputStream childIn = process.getInputStream();BufferedReader in = new BufferedReader(new InputStreamReader(childIn));String line = "";logger.info("Shell process in.readLine :>>>>>>>>>>>>>>>>:{}" , in.readLine());while ((line = in.readLine()) != null) {logger.info("runShell: {}" ,line);}BufferedReader stderrReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));while ((line = stderrReader.readLine()) != null) {logger.info("runShell: {}" , line);}int exitValue = process.waitFor();//脚本正确执行返回值为0logger.info("***执行结果***:{}" , exitValue);childIn.close();in.close();stderrReader.close();process.destroy();return true;}
}

文件的AES加解密

package com.fanghui.vota.packages.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.GeneralSecurityException;/*** 文件的AES加解密** @Author: fh* @Date: 2019-05-24 18:46*/
public class AesFileUtil {private static Logger logger = LoggerFactory.getLogger(AesFileUtil.class);/*** 分段加密大小*/final static int SEGSIZE = 1024 * 1024;/*** AES加密算法*/private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";/*** AES分段文件加密** @param file* @param encryptedFile* @param password* @return*/public static boolean fileSegAesEncrypt(File file, String encryptedFile, String password) {if (file == null || !file.exists()) {logger.error("fileSegAesEncrypt error,encrypt file is not exit");return false;}FileInputStream is = null;FileOutputStream output = null;try {//输入文件is = new FileInputStream(file);// 输出到文件output = new FileOutputStream(new File(encryptedFile));byte[] buf = new byte[SEGSIZE];int numBytesRead = 0;while ((numBytesRead = is.read(buf)) != -1) {if (numBytesRead != SEGSIZE) {byte[] data = new byte[numBytesRead];System.arraycopy(buf, 0, data, 0, numBytesRead);byte[] encrypt = aesEncrypt(data, password);output.write(encrypt, 0, encrypt.length);continue;}byte[] encrypt = aesEncrypt(buf, password);output.write(encrypt, 0, encrypt.length);}} catch (IOException | GeneralSecurityException e) {logger.error("error:" + e);logger.error("fileSegAesEncrypt error:" + e.getMessage());return false;} finally {closeStream(is, output);}return true;}private static void closeStream(FileInputStream is, FileOutputStream output) {if (is != null) {try {is.close();} catch (IOException e) {logger.error("error:" + e);}}if (output != null) {try {output.close();} catch (IOException e) {logger.error("error:" + e);}}}/*** AES文件加密** @param file* @param encryptedFile* @param password* @return*/public static boolean fileAesEncrypt(File file, String encryptedFile, String password) {if (file == null || !file.exists()) {logger.error("fileAesEncrypt error,encrypt file is not exit");return false;}FileInputStream is = null;FileOutputStream os = null;try {byte[] source = new byte[(int) file.length()];is = new FileInputStream(file);is.read(source, 0, (int) file.length());// 加密byte[] enc;enc = aesEncrypt(source, password);// 输出到文件os = new FileOutputStream(new File(encryptedFile));os.write(enc, 0, enc.length);} catch (IOException | GeneralSecurityException e) {logger.error("fileAesEncrypt error:" + e);return false;} finally {closeStream(is, os);}return true;}private static byte[] aesEncrypt(byte[] source, String password) throws GeneralSecurityException, UnsupportedEncodingException {byte[] strDefaultKey = password.getBytes("UTF-8");// 处理密钥SecretKeySpec key = new SecretKeySpec(strDefaultKey, "AES");// 加密Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, key);return cipher.doFinal(source);}/*** 对AES加密后的文件进行解密** @param encryptedFile* @param decryptedFile* @param password* @return*/public static boolean fileAesDecrypt(File encryptedFile, String decryptedFile, String password) {if (encryptedFile == null || !encryptedFile.exists()) {logger.error("fileAesDecrypt error,encryptedFile is not exit");return false;}FileInputStream is = null;FileOutputStream os = null;try {byte[] data = new byte[(int) encryptedFile.length()];is = new FileInputStream(encryptedFile);is.read(data, 0, (int) encryptedFile.length());// 加密byte[] dec;dec = aesDecrypt(data, password);// 输出到文件os = new FileOutputStream(new File(decryptedFile));os.write(dec, 0, dec.length);} catch (IOException | GeneralSecurityException e) {logger.error("fileAesDecrypt error:" + e);return false;} finally {closeStream(is, os);}return true;}/*** 对AES分段加密好的文件进行解密** @param encryptedFile 待解密文件* @param decryptedFile 解密后输出文件* @param password 密钥*/public static boolean fileSegAesDecrypt(File encryptedFile, String decryptedFile, String password) {if (encryptedFile == null || !encryptedFile.exists()) {logger.error("fileSegAesDecrypt error,encryptedFile is not exit");return false;}FileInputStream is = null;FileOutputStream output = null;try {//输入文件is = new FileInputStream(encryptedFile);// 输出到文件output = new FileOutputStream(new File(decryptedFile));byte[] buf = new byte[SEGSIZE + 16];int numBytesRead = 0;while ((numBytesRead = is.read(buf)) != -1) {if (numBytesRead != SEGSIZE) {byte[] data = new byte[numBytesRead];System.arraycopy(buf, 0, data, 0, numBytesRead);byte[] encrypt = aesDecrypt(data, password);output.write(encrypt, 0, encrypt.length);continue;}byte[] encrypt = aesDecrypt(buf, password);output.write(encrypt, 0, encrypt.length);}} catch (IOException | GeneralSecurityException e) {logger.error("fileSegAesDecrypt error:{}",e);return false;} finally {closeStream(is, output);}return true;}private static byte[] aesDecrypt(byte[] data, String password) throws GeneralSecurityException, UnsupportedEncodingException {// 处理密钥byte[] strDefaultKey = password.getBytes("UTF-8");SecretKeySpec key = new SecretKeySpec(strDefaultKey, "AES");// 解密Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, key);return cipher.doFinal(data);}public static void main(String[] args) {
//        File fileOrg = new File("D:\\差分工具包\\orgFile");
//        String password = "f70e300942ea540ed365fe0eb05b5585";
//        String encPath = "D:\\差分工具包\\java_enc_File";
//        fileSegAesEncrypt(fileOrg, encPath, password);
//
//        String deEncPath = "D:\\差分工具包\\de_java_enc_File";
//        fileSegAesDecrypt(new File(encPath), deEncPath, password);}
}

压缩工具类

package com.fanghui.vota.packages.util;import com.alibaba.cloud.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;public class ZipUtil {private static Logger logger = LoggerFactory.getLogger(ZipUtil.class);private static final int BUFF_SIZE = 4 * 1024;/*** 解压** @param zipFilePath 压缩文件* @param unzipPath   解压路径* @return return true if success*/public static String unzip(String zipFilePath, String unzipPath) throws IOException {File zFile = new File(zipFilePath);// 如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径String fileName = zFile.getName();if (StringUtils.isNotEmpty(fileName)) {fileName = fileName.substring(0, fileName.lastIndexOf("."));}unzipPath = unzipPath + fileName + "/";logger.info("**解压准备**" + unzipPath);ZipFile zipFile = null;try {zipFile = new ZipFile(zipFilePath, Charset.forName("GBK"));Enumeration emu = zipFile.entries();int i = 0;while (emu.hasMoreElements()) {ZipEntry entry = (ZipEntry) emu.nextElement();if (entry.isDirectory()) {new File(unzipPath + entry.getName()).mkdirs();continue;}BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));File file = new File(unzipPath + entry.getName());File parent = file.getParentFile();if (parent != null && (!parent.exists())) {parent.mkdirs();}FileOutputStream fos = null;BufferedOutputStream bos = null;try {fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);int count;byte[] data = new byte[BUFF_SIZE];while ((count = bis.read(data, 0, BUFF_SIZE)) != -1) {bos.write(data, 0, count);}bos.flush();} catch (IOException e) {e.printStackTrace();} finally {if (Objects.nonNull(bos)) {bos.close();}if (Objects.nonNull(fos)) {fos.close();}}}logger.info("**解压完成**: {}", unzipPath);return unzipPath;} catch (Exception e) {logger.info(String.valueOf(e));logger.info("ZipUtil unzip error! zip file:{},unzip to path:{}", zipFilePath ,unzipPath);return "";} finally {if (zipFile != null) {zipFile.close();}}}
}

相关文章:

加解密算法+压缩工具

sha256 工具类 package com.fanghui.vota.packages.util;import org.slf4j.Logger; import org.slf4j.LoggerFactory;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.math.BigInteger…...

FeignClient接口的几种方式总结

FeignClient这个注解&#xff0c;已经封装了远程调用协议。在springboot的开发&#xff0c;或者微服务的开发过程中&#xff0c;我们需要跨服务调用&#xff0c;或者调用外部的接口&#xff0c;我们都可以使用FeignClient。 一、FeignClient介绍 FeignClient 注解是 Spring Cl…...

springBoot多数据源使用tdengine(3.0.7.1)+MySQL+mybatisPlus+druid连接池

一、安装部署 1、我这里使用的 3.0.7.1版本&#xff0c;因为我看3.x版本已经发布了一年了&#xff0c;增加了很多新的功能&#xff0c;而且3.x官方推荐&#xff0c;对于2.x的版本&#xff0c;官网都已经推荐进行升级到3.x&#xff0c;所以考虑到项目以后的发展&#xff0c;决定…...

剑指Offer 05.替换空格

剑指Offer 05.替换空格 目录 剑指Offer 05.替换空格05.替换空格题目代码&#xff08;容易想到的&#xff09;利用库函数的方法题解&#xff08;时间复杂度更低&#xff09;面试&#xff1a;为什么java中String类型是不可变的 05.替换空格 题目 官网题目地址 代码&#xff08;…...

ChatGPT的功能与特点

随着人工智能技术的不断发展&#xff0c;ChatGPT作为OpenAI公司开发的基于GPT-3.5架构的大型语言模型&#xff0c;正引领着智能交互的新纪元。ChatGPT的功能与特点使其能够在多个领域展现出惊人的能力&#xff0c;本文将深入探讨ChatGPT的功能与特点&#xff0c;以及它在人工智…...

Vue2.0基础

1、概述 Vue(读音/vju/&#xff0c;类似于view)是一套用于构建用户界面的渐进式框架&#xff0c;发布于2014年2月。与其它大型框架不同的是&#xff0c;Vue被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层&#xff08;也就是可以理解为HTMLCSSJS&#xff09;&#xff…...

rust 如何定义[u8]数组?

在Rust中&#xff0c;有多种方式可以定义 [u8] 数组。以下是一些常见的方式&#xff1a; 使用数组字面量初始化数组&#xff1a; let array: [u8; 5] [1, 2, 3, 4, 5];使用 vec! 宏创建可变长度的数组&#xff1a; let mut vec: Vec<u8> vec![1, 2, 3, 4, 5];使用 v…...

关于Hive的使用技巧

前言 Hive是一个基于Hadoop的数据仓库基础架构&#xff0c;它提供了一种类SQL的查询语言&#xff0c;称为HiveQL&#xff0c;用于分析和处理大规模的结构化数据。 Hive的主要特点包括&#xff1a; 可扩展性&#xff1a;Hive可以处理大规模的数据&#xff0c;支持高性能的并行…...

【C++】BSTree 模拟笔记

文章目录 概念插入和删除非递归实现中的问题递归中的引用简化相关OJ复习直达 概念 由下面二叉搜索树的性质可以知道&#xff0c;中序遍历它便可以得到一个升序序列&#xff0c;查找效率高&#xff0c;小于往左找&#xff0c;大于往右走。最多查找高度次&#xff0c;走到到空&am…...

5分钟快手入门laravel邮件通知

第一步&#xff1a; 生成一个邮件发送对象 php artisan make:mail TestMail 第二步&#xff1a; 编辑.env 添加/修改&#xff08;没有的key则添加&#xff09; MAIL_DRIVERsmtp MAIL_HOSTsmtp.163.com &#xff08;这里用163邮箱&#xff09; MAIL_PORT25 &#xff08;163邮箱…...

iOS——Block two

Block 的实质究竟是什么呢&#xff1f;类型&#xff1f;变量&#xff1f;还是什么黑科技&#xff1f; Blocks 是 带有局部变量的匿名函数 Blocks 由 OC 转 C 源码方法 在项目中添加 blocks.m 文件&#xff0c;并写好 block 的相关代码。打开「终端」&#xff0c;执行 cd XX…...

Ubuntu出现内部错误解决办法

使用的Ubuntu版本是18.04&#xff0c;使用的时候弹出对话框说出现了内部错误&#xff0c;好奇是哪里出现了错误&#xff0c;查找了一下解决的办法&#xff0c;记录一下。 参考解决方案&#xff1a;ubantu出现了内部错误 一旦程序崩溃过一次&#xff0c;就会生成一个.crash文件…...

2023年中职组“网络安全”赛项吉安市竞赛任务书

2023年中职组“网络安全”赛项 吉安市竞赛任务书 一、竞赛时间 总计&#xff1a;360分钟 竞赛阶段 竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值 A模块 A-1 登录安全加固 180分钟 200分 A-2 本地安全策略配置 A-3 流量完整性保护 A-4 事件监控 A-5 服务加固…...

ELK日志分析系统介绍及搭建(超详细)

目录 一、ELK日志分析系统简介 二、Elasticsearch介绍 2.1Elasticsearch概述 三、Logstash介绍 四、Kibana介绍 五、ELK工作原理 六、部署ELK日志分析系统 6.1ELK Elasticsearch 集群部署&#xff08;在Node1、Node2节点上操作&#xff09; 6.2部署 Elasticsearch 软件 …...

docker 资源限制

目录 1、CPU使用率 2、CPU共享比例 3、CPU周期限制 4、CPU核心限制 5、CPU 配额控制参数的混合案例 6、内存限制 7、Block IO 的限制 8、限制bps 和iops docker资源限制 Docker容器技术底层是通过Cgroup&#xff08;Control Group 控制组&#xff09;实现容器对物理资…...

HCIP 交换综合实验--企业三层架构

题目 1、内网IP地址使用172.16.0.0/26分配 2、SW1和SW2之间互为备份 3、VRRP/STP/VLAN/Eth-trunk均使用 4、所有PC均通过DHCP获取IP地址 5、ISP只能配置IP地址 6、所有电脑可以正常访问ISP路由器环回 实验步骤 第一步、规划IP地址 R1-R2&#xff1a;100.1.1.0/24 R2-LSW1…...

微服务的基础使用

微服务 Maven的依赖冲突解决方案&#xff1a; 路径最短原则 配置优先原则 破坏规则则使用排除 SpringBoot场景启动器starter的开发流程 c3p0-spring-boot-starter自定义场景启动器 test-c3p0调用自定义场景启动器 SpringBoot自动装配 SpringBoot应用启动原理 nacos服务治…...

opencv-29 Otsu 处理(图像分割)

Otsu 处理 Otsu 处理是一种用于图像分割的方法&#xff0c;旨在自动找到一个阈值&#xff0c;将图像分成两个类别&#xff1a;前景和背景。这种方法最初由日本学者大津展之&#xff08;Nobuyuki Otsu&#xff09;在 1979 年提出 在 Otsu 处理中&#xff0c;我们通过最小化类别内…...

网络中通过IP地址查找位置

display ip routing-table 查看路由表 display vlan 查看vlan 信息 display stp brief 查看生成树信息 display mac-address 查看mac 地址表 display arp 查看arp表 SW1 SW2...

MyBatis的动态SQL语句

文章目录 前言LocalDate数据库代码po 包 ifwhere 标签 查trim 标签 增set 标签 改foreach 标签 删 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 查询条件是动态的 MyBatis的动态SQL语句是指在运行时根据不同条件选择不同的SQL语句执行。 这些条件可…...

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

为什么需要建设工程项目管理?工程项目管理有哪些亮点功能?

在建筑行业&#xff0c;项目管理的重要性不言而喻。随着工程规模的扩大、技术复杂度的提升&#xff0c;传统的管理模式已经难以满足现代工程的需求。过去&#xff0c;许多企业依赖手工记录、口头沟通和分散的信息管理&#xff0c;导致效率低下、成本失控、风险频发。例如&#…...

蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练

前文覆盖了 BLE 扫描的基础概念与经典问题蓝牙 BLE 扫描面试题大全(1)&#xff1a;从基础到实战的深度解析-CSDN博客&#xff0c;但实际面试中&#xff0c;企业更关注候选人对复杂场景的应对能力&#xff08;如多设备并发扫描、低功耗与高发现率的平衡&#xff09;和前沿技术的…...

群晖NAS如何在虚拟机创建飞牛NAS

套件中心下载安装Virtual Machine Manager 创建虚拟机 配置虚拟机 飞牛官网下载 https://iso.liveupdate.fnnas.com/x86_64/trim/fnos-0.9.2-863.iso 群晖NAS如何在虚拟机创建飞牛NAS - 个人信息分享...

MacOS下Homebrew国内镜像加速指南(2025最新国内镜像加速)

macos brew国内镜像加速方法 brew install 加速formula.jws.json下载慢加速 &#x1f37a; 最新版brew安装慢到怀疑人生&#xff1f;别怕&#xff0c;教你轻松起飞&#xff01; 最近Homebrew更新至最新版&#xff0c;每次执行 brew 命令时都会自动从官方地址 https://formulae.…...

Linux中《基础IO》详细介绍

目录 理解"文件"狭义理解广义理解文件操作的归类认知系统角度文件类别 回顾C文件接口打开文件写文件读文件稍作修改&#xff0c;实现简单cat命令 输出信息到显示器&#xff0c;你有哪些方法stdin & stdout & stderr打开文件的方式 系统⽂件I/O⼀种传递标志位…...

高效的后台管理系统——可进行二次开发

随着互联网技术的迅猛发展&#xff0c;企业的数字化管理变得愈加重要。后台管理系统作为数据存储与业务管理的核心&#xff0c;成为了现代企业不可或缺的一部分。今天我们要介绍的是一款名为 若依后台管理框架 的系统&#xff0c;它不仅支持跨平台应用&#xff0c;还能提供丰富…...

6.9本日总结

一、英语 复习默写list11list18&#xff0c;订正07年第3篇阅读 二、数学 学习线代第一讲&#xff0c;写15讲课后题 三、408 学习计组第二章&#xff0c;写计组习题 四、总结 明天结束线代第一章和计组第二章 五、明日计划 英语&#xff1a;复习l默写sit12list17&#…...

NineData数据库DevOps功能全面支持百度智能云向量数据库 VectorDB,助力企业 AI 应用高效落地

NineData 的数据库 DevOps 解决方案已完成对百度智能云向量数据库 VectorDB 的全链路适配&#xff0c;成为国内首批提供 VectorDB 原生操作能力的服务商。此次合作聚焦 AI 开发核心场景&#xff0c;通过标准化 SQL 工作台与细粒度权限管控两大能力&#xff0c;助力企业安全高效…...

解密鸿蒙系统的隐私护城河:从权限动态管控到生物数据加密的全链路防护

摘要 本文以健康管理应用为例&#xff0c;展示鸿蒙系统如何通过细粒度权限控制、动态权限授予、数据隔离和加密存储四大核心机制&#xff0c;实现复杂场景下的用户隐私保护。我们将通过完整的权限请求流程和敏感数据处理代码&#xff0c;演示鸿蒙系统如何平衡功能需求与隐私安…...