1.項目中引入jar包

javabase64-1.3.1.jar

2.如果是springboot項目,若在maven中不知道遠程路徑,可以按照如下方式操作;

2.1可以直接下載jar,

2.2在項目的src平級目錄下創建一個lib backage

2.3 在pom.xml

<dependency><groupId>it.sauronsoftware.base64</groupId><artifactId>base64</artifactId><version>1.3.1</version><scope>system</scope><systemPath>${basedir}/lib/javabase64-1.3.1.jar</systemPath>
</dependency>

這樣就可以引入了。

3.轉入正題,我們來講加密工具類

AESUtils
package com.foxconn.oem.common.utils.safe;import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.Key;
import java.security.SecureRandom;/*** <p>* AES加密解密工具包* </p>* * @author F3851884 XHB* @date 2012-5-18* @version 1.0*/
public class AESUtils {public static final String ALGORITHM = "AES";public static final int KEY_SIZE = 128;public static final int CACHE_SIZE = 1024;public static final String CODE = "utf-8";/*** <p>* 生成随机密钥* </p>* * @return* @throws Exception*/public static String getSecretKey() throws Exception {return getSecretKey(null);}/*** <p>* 生成密钥* </p>* * @param seed 密钥种子* @return* @throws Exception*/public static String getSecretKey(String seed) throws Exception {KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);SecureRandom secureRandom;if (seed != null && !"".equals(seed)) {//此种写法Windows上正常,Linux上异常//secureRandom = new SecureRandom(seed.getBytes());secureRandom = SecureRandom.getInstance("SHA1PRNG");secureRandom.setSeed(seed.getBytes());} else {secureRandom = new SecureRandom();}keyGenerator.init(KEY_SIZE, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); return Base64Utils.encode(secretKey.getEncoded());}/*** <p>* 加密* </p>** @param key* @return* @throws Exception*/public static String encrypt(String content, String key) throws Exception {if(content==null)content="";byte[] byteContent = content.getBytes(CODE);Key k = toKey(Base64Utils.decode(key));byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);byte[] result =cipher.doFinal(byteContent);//通过Base64转码返回return Base64Utils.encode(result);}/*** <p>* 文件加密* </p>* * @param key* @param sourceFilePath* @param destFilePath* @throws Exception*/public static void encryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {InputStream in = null;OutputStream out = null;CipherInputStream cin = null;try {File sourceFile = new File(sourceFilePath);File destFile = new File(destFilePath);if (sourceFile.exists() && sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {destFile.getParentFile().mkdirs();}destFile.createNewFile();in = new FileInputStream(sourceFile);out = new FileOutputStream(destFile);Key k = toKey(Base64Utils.decode(key));byte[] raw = k.getEncoded();SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);cin = new CipherInputStream(in, cipher);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = cin.read(cache)) != -1) {out.write(cache, 0, nRead);out.flush();}}}catch (Exception e){e.printStackTrace();}finally {if(out != null){try {out.close();}catch (Exception e){}}if(cin != null){try {cin.close();}catch (Exception e){}}if(in != null){try {in.close();}catch (Exception e){}}}}/*** <p>* 解密* </p>** @param key* @return* @throws Exception*/public static String decrypt(String content, String key) throws Exception {Key k = toKey(Base64Utils.decode(key));byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);byte[] result = cipher.doFinal(Base64Utils.decode(content));return new String(result, CODE);}/*** <p>* 文件解密* </p>* * @param key* @param sourceFilePath* @param destFilePath* @throws Exception*/public static void decryptFile(String key, String sourceFilePath, String destFilePath) throws Exception {FileInputStream in = null;FileOutputStream out = null;CipherOutputStream cout = null;try {File sourceFile = new File(sourceFilePath);File destFile = new File(destFilePath);if (sourceFile.exists() && sourceFile.isFile()) {if (!destFile.getParentFile().exists()) {destFile.getParentFile().mkdirs();}destFile.createNewFile();in = new FileInputStream(sourceFile);out = new FileOutputStream(destFile);Key k = toKey(Base64Utils.decode(key));byte[] raw = k.getEncoded();SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);Cipher cipher = Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);cout = new CipherOutputStream(out, cipher);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = in.read(cache)) != -1) {cout.write(cache, 0, nRead);cout.flush();}}}catch (Exception e){e.printStackTrace();}finally {if(cout != null){try {cout.close();}catch (Exception e){}}if(out != null){try {out.close();}catch (Exception e){}}if(in != null){try {in.close();}catch (Exception e){}}}}/*** <p>* 转换密钥* </p>* * @param key* @return* @throws Exception*/public static Key toKey(byte[] key) throws Exception {SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);return secretKey;}}

3.1

package com.foxconn.oem.common.utils.safe;import it.sauronsoftware.base64.Base64;import java.io.*;/**
* <p>
* BASE64编码解码工具包
* </p>
* <p>
* 依赖javabase64-1.3.1.jar
* </p>
*
* @author F3851884 XHB
* @date 2012-5-19
* @version 1.0
*/
public class Base64Utils {/*** 文件读取缓冲区大小*/private static final int CACHE_SIZE = 1024;/*** <p>* BASE64字符串解码为二进制数据* </p>** @param base64* @return* @throws Exception*/public static byte[] decode(String base64) throws Exception {return Base64.decode(base64.getBytes());}/*** <p>* 二进制数据编码为BASE64字符串* </p>** @param bytes* @return* @throws Exception*/public static String encode(byte[] bytes) throws Exception {return new String(Base64.encode(bytes));}/*** <p>* 将文件编码为BASE64字符串* </p>* <p>* 大文件慎用,可能会导致内存溢出* </p>** @param filePath 文件绝对路径* @return* @throws Exception*/public static String encodeFile(String filePath) throws Exception {byte[] bytes = fileToByte(filePath);return encode(bytes);}/*** <p>* BASE64字符串转回文件* </p>** @param filePath 文件绝对路径* @param base64 编码字符串* @throws Exception*/public static void decodeToFile(String filePath, String base64) throws Exception {byte[] bytes = decode(base64);byteArrayToFile(bytes, filePath);}/*** <p>* 文件转换为二进制数组* </p>** @param filePath 文件路径* @return* @throws Exception*/public static byte[] fileToByte(String filePath) throws Exception {byte[] data = new byte[0];FileInputStream in = null;ByteArrayOutputStream out = null;try {File file = new File(filePath);if (file.exists()) {in = new FileInputStream(file);out = new ByteArrayOutputStream(2048);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = in.read(cache)) != -1) {out.write(cache, 0, nRead);out.flush();}data = out.toByteArray();}}catch (Exception e){e.printStackTrace();}finally {if(out != null){try {out.close();}catch (Exception e){}}if(in != null){try {in.close();}catch (Exception e){}}}return data;}/*** <p>* 二进制数据写文件* </p>** @param bytes 二进制数据* @param filePath 文件生成目录*/public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {InputStream in = null;OutputStream out = null;try {in = new ByteArrayInputStream(bytes);File destFile = new File(filePath);if (!destFile.getParentFile().exists()) {destFile.getParentFile().mkdirs();}destFile.createNewFile();out = new FileOutputStream(destFile);byte[] cache = new byte[CACHE_SIZE];int nRead = 0;while ((nRead = in.read(cache)) != -1) {out.write(cache, 0, nRead);out.flush();}}catch (Exception e){e.printStackTrace();}finally {if(out != null){try {out.close();}catch (Exception e){}}if(in != null){try {in.close();}catch (Exception e){}}}}}

java中常用的加密工具類相关推荐

  1. java中常用的加密工具

    java中常用的加密工具 1. md5加密工具类 public class MD5Utils {private static final String hexDigIts[] = {"0&q ...

  2. java中常用的日期工具类

    java中常用的日期工具类 日期相关的类: package net.yto.ofclacct.core.util;import java.text.ParseException; import jav ...

  3. Java中常用的加密方法(JDK)

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  4. Java中常用的 JSON工具

    Spring Cloud 2.x系列之网关zuul入门(三) Spring Cloud 2.x系列之网关zuul入门(二) 本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能. 每次都在 ...

  5. java中常用的加密方法_java中常用的数据加密算法

    [项目中第一次深入地了解到加密算法的使用,现第一阶段结束,将使用到的加密算法和大家分享一下:首先还是先给大家普及一下常用加密算法的基础知识基本的单向加密算法BASE6 以下为加密的工具类: impor ...

  6. Java中常用的加密方式

    一.Java常用加密方式 Base64加密算法(编码方式) MD5加密(消息摘要算法,验证信息完整性) 对称加密算法 非对称加密算法 数字签名算法 数字证书 二.分类 按加密算法是否需要key被分为两 ...

  7. Java中常用的加密与解密

    一.对称加密与非对称加密 1.对称加密 对称加密采用了对称密码编码技术,它的特点是文件加密和解密使用相同的密钥加密,这种方法在密码学中叫做对称加密算法,对称加密算法使用起来简单快捷,密钥较短,且破译困 ...

  8. java中常用到的工具类使用

    Tool 不定期更新,建议收藏,收录日常所用 1,判断对象是否为空的常用工具类 2,对象和数组的复制 3,关于拼接字符串去掉最后一个符号的三种方式 4,判断对象值属性不为null并且不为空字符串 5, ...

  9. Java中常用的测试工具JUnit

    使用步骤 1.新建一个test源文件夹(SourceFolder),避免和业务代码的src混在一起 2.在test下新建JUnit Test Case 3.相关测试代码放到一个Test Case,每个 ...

最新文章

  1. Dependency injection in ASP.NET Core
  2. 深刻理解:反向代理服务器
  3. 1893. 检查是否区域内所有整数都被覆盖
  4. 高中学生计算机软件,中学生计算器
  5. 查看SQL执行计划的方法及优劣
  6. 产品工作中/阅读中的涓滴意念
  7. 华盛顿大学成立SAML实验室:陈天奇参与,推进未来AI系统全栈研究
  8. 关于YOLOv3的文章
  9. vlc linux静态版本,vlc 在ubuntu 14下的linux版本编译
  10. 高等数学下——平面与直线
  11. LCD1602液晶显示屏驱动文件
  12. 用 JS 给图片加文字水印或图片水印
  13. deepin装oracle,deepin安装Oracle jdk8,以及添加add-apt-repository命令支持
  14. win10开启快速启动,关机时电源键一直亮着无法正常关机。。。
  15. 【致远FAQ】V5V8.0sp1_单位管理员-流程督办监控-批量移交-待分配事项,是什么意思?
  16. Android国际化,简体和繁体切换
  17. 基于C++的《元素战争》基于win32框架的电脑游戏设计
  18. win7java浏览器崩溃_win7系统IE浏览器出现各种崩溃问题的解决方法
  19. KAIST URBAN DATA SET/Complex Urban LiDAR Data Set数据集参数
  20. 2019年诺贝尔生理医学奖揭晓 |动图展示历年生理学奖

热门文章

  1. 事件分发机制原理及其分析
  2. CentOS中如何检测IP冲突
  3. Fielding博士论文导读----第6章 1
  4. nextval数组求解
  5. BI现状和未来发展趋势
  6. 齐治科技“数据中心资产安全管理系统”首发面世
  7. Excel VBA | 自动添加序列号
  8. 构造“质量、安全”的管理体系-等保测评为您排忧解难
  9. 简明python教程英语版_简明Python教程(A Byte of Python中文版)
  10. 绞车提升能力及钢丝绳验算软件验算结果如下