java 加密解密工具类(实用!!!)
最近发现了一个加密解密的好例子,很方便使用,可以作为平时开发的工具集,记录一下。
package com.sh.springboottdemo2.util; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom; public class EncryptUtil {
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA1";
public static final String HmacMD5 = "HmacMD5";
public static final String HmacSHA1 = "HmacSHA1";
public static final String DES = "DES";
public static final String AES = "AES"; /**编码格式;默认使用uft-8*/
public String charset = "utf-8";
/**DES*/
public int keysizeDES = 0;
/**AES*/
public int keysizeAES = 128; public static EncryptUtil me; private EncryptUtil(){
//单例
}
//双重锁
public static EncryptUtil getInstance(){
if (me==null) {
synchronized (EncryptUtil.class) {
if(me == null){
me = new EncryptUtil();
}
}
}
return me;
} /**
* 使用MessageDigest进行单向加密(无密码)
* @param res 被加密的文本
* @param algorithm 加密算法名称
* @return
*/
private String messageDigest(String res,String algorithm){
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);
return base64(md.digest(resBytes));
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 使用KeyGenerator进行单向/双向加密(可设密码)
* @param res 被加密的原文
* @param algorithm 加密使用的算法名称
* @param key 加密使用的秘钥
* @return
*/
private String keyGeneratorMac(String res,String algorithm,String key){
try {
SecretKey sk = null;
if (key==null) {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
sk = kg.generateKey();
}else {
byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
sk = new SecretKeySpec(keyBytes, algorithm);
}
Mac mac = Mac.getInstance(algorithm);
mac.init(sk);
byte[] result = mac.doFinal(res.getBytes());
return base64(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 使用KeyGenerator双向加密,DES/AES,注意这里转化为字符串的时候是将2进制转为16进制格式的字符串,不是直接转,因为会出错
* @param res 加密的原文
* @param algorithm 加密使用的算法名称
* @param key 加密的秘钥
* @param keysize
* @param isEncode
* @return
*/
private String keyGeneratorES(String res,String algorithm,String key,int keysize,boolean isEncode){
try {
KeyGenerator kg = KeyGenerator.getInstance(algorithm);
if (keysize == 0) {
byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
kg.init(new SecureRandom(keyBytes));
}else if (key==null) {
kg.init(keysize);
}else {
byte[] keyBytes = charset==null?key.getBytes():key.getBytes(charset);
kg.init(keysize, new SecureRandom(keyBytes));
}
SecretKey sk = kg.generateKey();
SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
Cipher cipher = Cipher.getInstance(algorithm);
if (isEncode) {
cipher.init(Cipher.ENCRYPT_MODE, sks);
byte[] resBytes = charset==null?res.getBytes():res.getBytes(charset);
return parseByte2HexStr(cipher.doFinal(resBytes));
}else {
cipher.init(Cipher.DECRYPT_MODE, sks);
return new String(cipher.doFinal(parseHexStr2Byte(res)));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} private String base64(byte[] res){
return Base64.encode(res);
} /**将二进制转换成16进制 */
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
} /**
* md5加密算法进行加密(不可逆)
* @param res 需要加密的原文
* @return
*/
public String MD5(String res) {
return messageDigest(res, MD5);
} /**
* md5加密算法进行加密(不可逆)
* @param res 需要加密的原文
* @param key 秘钥
* @return
*/
public String MD5(String res, String key) {
return keyGeneratorMac(res, HmacMD5, key);
} /**
* 使用SHA1加密算法进行加密(不可逆)
* @param res 需要加密的原文
* @return
*/
public String SHA1(String res) {
return messageDigest(res, SHA1);
} /**
* 使用SHA1加密算法进行加密(不可逆)
* @param res 需要加密的原文
* @param key 秘钥
* @return
*/
public String SHA1(String res, String key) {
return keyGeneratorMac(res, HmacSHA1, key);
} /**
* 使用DES加密算法进行加密(可逆)
* @param res 需要加密的原文
* @param key 秘钥
* @return
*/
public String DESencode(String res, String key) {
return keyGeneratorES(res, DES, key, keysizeDES, true);
} /**
* 对使用DES加密算法的密文进行解密(可逆)
* @param res 需要解密的密文
* @param key 秘钥
* @return
*/
public String DESdecode(String res, String key) {
return keyGeneratorES(res, DES, key, keysizeDES, false);
} /**
* 使用AES加密算法经行加密(可逆)
* @param res 需要加密的密文
* @param key 秘钥
* @return
*/
public String AESencode(String res, String key) {
return keyGeneratorES(res, AES, key, keysizeAES, true);
} /**
* 对使用AES加密算法的密文进行解密
* @param res 需要解密的密文
* @param key 秘钥
* @return
*/
public String AESdecode(String res, String key) {
return keyGeneratorES(res, AES, key, keysizeAES, false);
} /**
* 使用异或进行加密
* @param res 需要加密的密文
* @param key 秘钥
* @return
*/
public String XORencode(String res, String key) {
byte[] bs = res.getBytes();
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return parseByte2HexStr(bs);
} /**
* 使用异或进行解密
* @param res 需要解密的密文
* @param key 秘钥
* @return
*/
public String XORdecode(String res, String key) {
byte[] bs = parseHexStr2Byte(res);
for (int i = 0; i < bs.length; i++) {
bs[i] = (byte) ((bs[i]) ^ key.hashCode());
}
return new String(bs);
} /**
* 直接使用异或(第一调用加密,第二次调用解密)
* @param res 密文
* @param key 秘钥
* @return
*/
public int XOR(int res, String key) {
return res ^ key.hashCode();
} /**
* 使用Base64进行加密
* @param res 密文
* @return
*/
public String Base64Encode(String res) {
return Base64.encode(res.getBytes());
} /**
* 使用Base64进行解密
* @param res
* @return
*/
public String Base64Decode(String res) {
return new String(Base64.decode(res));
}
}
java 加密解密工具类(实用!!!)的更多相关文章
- java加密解密工具类
package com.founder.mrp.util; import java.nio.charset.StandardCharsets; import java.security.Key; im ...
- Base64加密解密工具类
使用Apache commons codec类Base64进行加密解密 maven依赖 <dependency> <groupId>commons-codec</grou ...
- .Net(c#)加密解密工具类:
/// <summary> /// .Net加密解密帮助类 /// </summary> public class NetCryptoHelper { #region des实 ...
- 加密解密工具类(Java,DES)
一个Java版的DES加密工具类,能够用来进行网络传输数据加密,保存password的时候进行加密. import java.security.Key; import java.security.sp ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- des 加密解密工具类
最近在做des的双对称加密解密,特此记录一下. des对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码 ...
- java中加密解密工具类
在工作中经常遇到需要加密.解密的场景.例如用户的手机号等信息,在保存到数据库的过程中,需要对数据进行加密.取出时进行解密. public class DEStool { private String ...
- 自写AES加密解密工具类
此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...
- Java-DES算法加密解密工具类
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import ...
随机推荐
- [iOS微博项目 - 4.5] - 每条微博的底部工具条
github: https://github.com/hellovoidworld/HVWWeibo A.每条微博的底部工具条 1.需求 每条微博底部都有一个工具条 显示3个按钮:评论.转发.赞 按钮 ...
- oracle 归档空间满的解决办法
问题现象: 通过命令提示符登陆数据库,一般提示“ora-03113:通信通道的文件结尾”错误,查看trace日志,可以看到详细信息.部分摘录如下(橙色部分给出了建议方案): Errors in fil ...
- Webpack4.x 入门
概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...
- 牛B三人组-快速排序-堆排序-归并排序
快速排序 随便取个数,作为标志值,这里就默认为索引位置为0的值 记录左索引和右索引,从右往左找比标志值小的,小值和左索引值交换,右索引变化,然后从左往右找比标志值大的,大值和右索引值交换,左索引变化 ...
- 剑指Offer——二叉树中和为某一值的路径
题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 分析: 先序遍历二叉树,找到二叉树中结点值的和 ...
- HBase简介及集群安装
一.Hbase概述 Apache HBase™是Hadoop数据库,是一个分布式,可扩展的大数据存储. 当您需要对大数据进行随机,实时读/写访问时,请使用Apache HBase™.该项目的目标是托 ...
- Python垃圾回收机制详解转自--Kevin Lu
一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存. #e ...
- jquery prop attr
checked比较特殊,只要设置了属性checked,不管何值都是checked的.例如:<input type="checkbox" checked><inpu ...
- 日期格式私人定制——SimpleDateFormat
[前言] 最近项目需要特殊的日期格式,又恰好是String类型的,以前都没怎么用到SimpleDateFormat这个类去格式化日期,脑子里蹦出来的思路就是先把Date给toString了,然后慢慢切 ...
- vs中nodejs代码 resharper 提示 ECMAScript2015 Feature. your Current language level is ECMAScript5的解决办法
问题如图 错误信息:ECMAScript 2015 Feature. your Current language level is: ECMAScript5 解决方法, 打开 Resharper -& ...