package com.founder.mrp.util;

import java.nio.charset.StandardCharsets;
import java.security.Key; import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec; import com.founder.mrp.web.jsonEntity.AccountJson; /**
* 加密解密类
* @author xiongzq
*
*/
public class EncryptionDecryption { private static String strDefaultKey = "UvVi-s`75X-d9fO3"; /** 加密工具 */
private ThreadLocal<Cipher> encryptCipher = new ThreadLocal<Cipher>() {
@Override
protected Cipher initialValue() {
try {
Cipher encryptCipher = Cipher.getInstance(algorithmName);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
return encryptCipher;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}; /** 解密工具 */
private ThreadLocal<Cipher> decryptCipher = new ThreadLocal<Cipher>() {
@Override
protected Cipher initialValue() {
try {
Cipher decryptCipher = Cipher.getInstance(algorithmName);
decryptCipher.init(Cipher.DECRYPT_MODE, key);
return decryptCipher;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}; private String algorithmName;
private Key key; /**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
*
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuilder sb = new StringBuilder(arrB.length * 2);
for (int b : arrB) {
int intTmp = b;
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
} /**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn 需要转换的字符串
* @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);
}
return arrOut;
} /**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public EncryptionDecryption() throws Exception {
this("AES/ECB/PKCS5Padding", strDefaultKey, 128);
} /**
* 指定密钥构造方法
*
* @param strKey 指定的密钥
* @throws Exception
*/
public EncryptionDecryption(String algorithmName, String strKey, int keySizeInBits) throws Exception {
this.algorithmName = algorithmName;
this.key = getKey(strKey.getBytes(StandardCharsets.UTF_8), keySizeInBits);
} /**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.get().doFinal(arrB);
} /**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
} /**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.get().doFinal(arrB);
} /**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
try {
return new String(decrypt(hexStr2ByteArr(strIn)));
} catch (Exception e) {
return "";
}
} /**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @param keySizeInBits
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp, int keySizeInBits) throws Exception {
// 创建一个空的字节数组(默认值为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 new SecretKeySpec(arrB, keyAlg);
}
}

java加密解密工具类的更多相关文章

  1. java 加密解密工具类(实用!!!)

    最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下. package com.sh.springboottdemo2.util; import com.sun.org.ap ...

  2. Base64加密解密工具类

    使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...

  3. .Net(c#)加密解密工具类:

    /// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...

  4. 加密解密工具类(Java,DES)

    一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...

  5. AES加密解密工具类封装(AESUtil)

    package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...

  6. des 加密解密工具类

    最近在做des的双对称加密解密,特此记录一下. des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码 ...

  7. java中加密解密工具类

    在工作中经常遇到需要加密.解密的场景.例如用户的手机号等信息,在保存到数据库的过程中,需要对数据进行加密.取出时进行解密. public class DEStool { private String ...

  8. 自写AES加密解密工具类

    此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...

  9. Java-DES算法加密解密工具类

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import ...

随机推荐

  1. WPF系列 —— 控件添加依赖属性(转)

    WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...

  2. uboot向kernel的传参机制——bootm与tags

    http://blog.csdn.net/skyflying2012/article/details/35787971 最近阅读代码学习了uboot boot kernel的过程以及uboot如何传参 ...

  3. LRU Algorithm Gym - 102394L (HASH)

    LRU Algorithm \[ Time Limit: 1000 ms\quad Memory Limit: 524288 kB \] 题意 给出 \(n\) 个数字和 \(m\) 次查询. 每次询 ...

  4. 二分查找java实现

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列. 二分查找思路非常简单,由粗暴的遍历查找改为 ...

  5. pycharm的Structure中的图标含义

    1.在使用pycharm的Structure时可以看到有这些图标.图标上的字母是简写,具体含义如下在pycharm中是自动补全的变量的类别  p:parameter 参数  m:method 方法   ...

  6. sublime3插件BracketHighlighter的配置

    BracketHighlighter插件能为Sublime Text提供括号,引号这类高亮功能,但安装此插件后,默认没有高亮,只有下划线表示,不是很醒目,需要配置:1.在Sublime Text中用p ...

  7. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于(WIFI模块AT指令TCP透传方式),定时访问升级

    前言 学习此代码所需: 实现功能概要 定时使用http访问云端的程序版本,如果版本不一致,然后通过http下载最新的升级文件,实现升级. 测试准备工作(默认访问我的服务器,改为自己的服务器,请看后面说 ...

  8. 微信小程序 scroll-view 横向滚动条 隐藏无效

    看了许多网上教程说是添加如下样式可以解决,我加入到组件wxss中无效,加入全局wxss生效. 添加css代码如下: ::-webkit-scrollbar { ; ; color: transpare ...

  9. #lua中编写shader的方式

    lua中编写shader的方式 1. 字符串拼接 类似于下面这种 vertDefaultSource = "\n".."\n" .. "attribu ...

  10. centos里的压缩解压命令tar总结

    压缩 tar czvf 压缩文件名称.tar.gz 文件或者目录名称 比如:tar czvf backup.tar.gz /etc,把/etc目录打包成文件backup.tar.gz c是打包 z是g ...