packagecom.founder.mrp.util;importjava.nio.charset.StandardCharsets;importjava.security.Key;importjavax.crypto.Cipher;importjavax.crypto.spec.SecretKeySpec;importcom.founder.mrp.web.jsonEntity.AccountJson;/*** 加密解密类

*@authorxiongzq

**/

public classEncryptionDecryption {private static String strDefaultKey = "UvVi-s`75X-d9fO3";/**加密工具*/

private ThreadLocal encryptCipher = new ThreadLocal() {

@OverrideprotectedCipher initialValue() {try{

Cipher encryptCipher=Cipher.getInstance(algorithmName);

encryptCipher.init(Cipher.ENCRYPT_MODE, key);returnencryptCipher;

}catch(Exception e) {

e.printStackTrace();return null;

}

}

};/**解密工具*/

private ThreadLocal decryptCipher = new ThreadLocal() {

@OverrideprotectedCipher initialValue() {try{

Cipher decryptCipher=Cipher.getInstance(algorithmName);

decryptCipher.init(Cipher.DECRYPT_MODE, key);returndecryptCipher;

}catch(Exception e) {

e.printStackTrace();return null;

}

}

};privateString algorithmName;privateKey key;/*** 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]

* hexStr2ByteArr(String strIn) 互为可逆的转换过程

*

*@paramarrB

* 需要转换的byte数组

*@return转换后的字符串

*@throwsException

**/

public static String byteArr2HexStr(byte[] arrB) throwsException {//每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍

StringBuilder sb = new StringBuilder(arrB.length * 2);for (intb : arrB) {int intTmp =b;//把负数转换为正数

while (intTmp < 0) {

intTmp= intTmp + 256;

}//小于0F的数需要在前面补0

if (intTmp < 16) {

sb.append("0");

}

sb.append(Integer.toString(intTmp,16));

}returnsb.toString();

}/*** 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)

* 互为可逆的转换过程

*

*@paramstrIn 需要转换的字符串

*@return转换后的byte数组

**/

public static byte[] hexStr2ByteArr(String strIn) {byte[] arrB =strIn.getBytes();int iLen =arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2

byte[] arrOut = new byte[iLen / 2];for (int i = 0; i < iLen; i = i + 2) {

String strTmp= new String(arrB, i, 2);

arrOut[i/ 2] = (byte) Integer.parseInt(strTmp, 16);

}returnarrOut;

}/*** 默认构造方法,使用默认密钥

*

*@throwsException*/

public EncryptionDecryption() throwsException {this("AES/ECB/PKCS5Padding", strDefaultKey, 128);

}/*** 指定密钥构造方法

*

*@paramstrKey 指定的密钥

*@throwsException*/

public EncryptionDecryption(String algorithmName, String strKey, int keySizeInBits) throwsException {this.algorithmName =algorithmName;this.key =getKey(strKey.getBytes(StandardCharsets.UTF_8), keySizeInBits);

}/*** 加密字节数组

*

*@paramarrB

* 需加密的字节数组

*@return加密后的字节数组

*@throwsException*/

public byte[] encrypt(byte[] arrB) throwsException {returnencryptCipher.get().doFinal(arrB);

}/*** 加密字符串

*

*@paramstrIn

* 需加密的字符串

*@return加密后的字符串

*@throwsException*/

public String encrypt(String strIn) throwsException {returnbyteArr2HexStr(encrypt(strIn.getBytes()));

}/*** 解密字节数组

*

*@paramarrB

* 需解密的字节数组

*@return解密后的字节数组

*@throwsException*/

public byte[] decrypt(byte[] arrB) throwsException {returndecryptCipher.get().doFinal(arrB);

}/*** 解密字符串

*

*@paramstrIn

* 需解密的字符串

*@return解密后的字符串

*@throwsException*/

public String decrypt(String strIn) throwsException {try{return newString(decrypt(hexStr2ByteArr(strIn)));

}catch(Exception e) {return "";

}

}/*** 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位

*

*@paramarrBTmp

* 构成该字符串的字节数组

*@paramkeySizeInBits

*@return生成的密钥

*@throwsjava.lang.Exception*/

private Key getKey(byte[] arrBTmp, int keySizeInBits) throwsException {//创建一个空的字节数组(默认值为0)

byte[] arrB = new byte[keySizeInBits / 8];//将原始字节数组转换为8位

for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {

arrB[i]=arrBTmp[i];

}//生成密钥

String keyAlg =algorithmName;int t = algorithmName.indexOf('/');if (t != -1) {

keyAlg= algorithmName.substring(0, t);

}return newSecretKeySpec(arrB, keyAlg);

}

}

java 加密解密 工具包_java加密解密工具类相关推荐

  1. java properties文件 安全_java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] | 学步园...

    java 数据库读取工具类(读取config.properties配置文件)[包含线程安全] 数据库读取工具类 package com.db; import java.sql.Connection; ...

  2. java前补零工具类_java生成编码工具类,不足补0

    ~~~~~ 小小工具类!你值得拥有 简单粗暴,直接上代码 import java.text.NumberFormat; /** * @author: Abner * @description: 编码工 ...

  3. Java - HuTool 使用 EscapeUtil、XmlUtil等工具类(四)

    Java - HuTool 使用 EscapeUtil.XmlUtil等工具类(四) 本篇主要介绍 HuTool工具, 其是 java工具类,对于一些静态方法进行封装,虽然很小,但很全,里面拥有平时我 ...

  4. java 手机号脱敏,身份证号脱敏 工具类

    java 手机号脱敏,身份证号脱敏 工具类 import org.apache.commons.lang3.StringUtils;/*** * @title: 脱敏工具类* @author: wll ...

  5. java将链接生成二维码工具类

    一.添加依赖 <!-- 生成二维码--><dependency><groupId>com.google.zxing</groupId><artif ...

  6. Java生成和解析二维码工具类(简单经典)

    Java生成和解析二维码工具类 开箱即用,简单不废话. pom.xml引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/ ...

  7. 记录一下:Java 汉字获取拼音或首字母工具类

    记录一下:Java 汉字获取拼音或首字母工具类 Maven依赖配置 Java代码 本文主要记录一下在Java中,如何将字符串中的中文转化为拼音,获取汉字串拼音首字母,获取汉字串拼音的工具类,以及相关的 ...

  8. JAVA之多sheet页表格生成工具类

    JAVA之多sheet页表格生成工具类 主要方法: import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.h ...

  9. java加密解密代码_java加解密文件公用方法整合(多看一本书,少写三行代码)

    最近接到任务(文件的安全性)需要在文件上传到服务器上时将文件加密保存, 用户下载时将文件解密后返回给用户.翻了下方法最后决定用java中的Cipher类来完成(里面的实现方式挺全的). 上手实现.po ...

最新文章

  1. Spring Cloud Gateway 雪崩了,该怎么办不要慌
  2. python windows窗口置顶_Python入门:第一个程序“Hello, world”
  3. Alpha阶段事后分析
  4. Boost1.62.0 + VS2015 配置
  5. DL框架之TensorFlow:深度学习框架TensorFlow Core(低级别TensorFlow API)的简介、安装、使用方法之详细攻略
  6. 详解边缘计算:为何而起、优势如何、哪些玩家以及正在爆发的场景
  7. java环境变量设置xp_java环境变量设置方法
  8. Xshell安装sql报错:······ RSA sha1 ((MD5) PGP) md5 NOT OK (MISSING KEYS: (MD5) PGP#3a79bd29)
  9. shell如何检测linux发行版本,shell判断软件版本
  10. python神奇功能_16个你毫不知道的Python神奇技能
  11. 使用Java进行RS232C端口的开发
  12. [python]python pandas 模块
  13. 小米8android版本打开,怎么查看小米手机安卓系统版本
  14. python获取京东服务器的毫秒级时间
  15. MINA框架客户端的使用
  16. 【Excel】偷懒小技巧2:快速为非空单元格编号
  17. Gazebo模型下载
  18. 智慧食堂到底如何运营?学校食堂必看
  19. 心理健康APP开发解决方案
  20. 理光Ricoh Aficio MP 2014 一体机驱动

热门文章

  1. ARM核心板有什么不同
  2. Wi n E x e c和O p e n F i l e等,只是为了实现与1 6位Wi n d o w s程 序的向后兼容而存在
  3. ROS2——DDS(十三)
  4. 八爪鱼南都行|人工智能助力智能门锁行业研究
  5. 清华大学2022就业报告出炉!
  6. 汇编 统计字符串大小写字母,数字,其他字符个数并输出到屏幕(简单实现,含详细注释)
  7. android炫彩文字和滚动的彩色背景
  8. 红外热像仪方便研发合理布局
  9. 深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
  10. 用Java做手机备忘录_基于安卓Android的手机备忘录设计(AndroidStudio)