package com.hengyu.ticket.util; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.security.Key; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.Base64; import java.util.Formatter; import java.util.UUID; public class SecurityHanlder { //加密类型 public enum KeyType{ AES,DES,DESede } private static final String uuid = "4c7a37a8-b8d7-442f-b9df-31ec4bcacebc"; public static String TYPE = KeyType.AES.name(); // optional value AES/DES/DESede /** * 获取 md5 加密字符串 * * @param str * @return * @throws Exception */ public static String md5(String str) throws Exception { return getSign(str, "MD5"); } /** * 获取 md5 加密字符串 * 规则 str+uuid * * @param str * @return * @throws Exception */ public static String md5x(String str) throws Exception { return getSign(str + uuid, "MD5"); } /** * 获取 sha1 加密字符串 * * @param str * @return * @throws Exception */ public static String sha1(String str) throws Exception { return getSign(str, "SHA-1"); } /** * 获取 sha1 加密字符串 * 二次加密 * 规则 str+uuid * * @param str * @return * @throws Exception */ public static String sha1x(String str) throws Exception { return getSign(str + uuid, "SHA-1"); } /** * 获取 sha1进行加密的字符后再进行 md5 加密字符串 * 二次加密 * * @param str * @return * @throws Exception */ public static String md5_sha1(String str) throws Exception { return getSign(getSign(str, "MD5"), "SHA-1"); } /** * 获取 md5 进行加密的字符后再进行 sha1加密字符串 * * @param str * @return * @throws Exception */ public static String sha1_md5(String str) throws Exception { return getSign(getSign(str, "SHA-1"), "MD5"); } /** * 创建随机字符 * * @return */ public static String createNonceStr() { return UUID.randomUUID().toString(); } /** * 获取加密签名 * * @param str 字符 * @param type 加密类型 * @return * @throws Exception */ public static String getSign(String str, String type) throws Exception { MessageDigest crypt = MessageDigest.getInstance(type); crypt.reset(); crypt.update(str.getBytes("UTF-8")); return str = byteToHex(crypt.digest()); } /** * 字节转换 16 进制 * * @param hash * @return */ private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); } String result = formatter.toString(); formatter.close(); return result; } /** * base64编码 * * @param source * @return */ public static String Base64Encoder(String source) { return new String(Base64.getEncoder().encode(source.getBytes())); } public static Key getSecretKey(String key) throws Exception{ SecretKey securekey = null; if(key == null){ key = ""; } KeyGenerator keyGenerator = KeyGenerator.getInstance(TYPE); keyGenerator.init(new SecureRandom(key.getBytes())); securekey = keyGenerator.generateKey(); return securekey; } /** * 文本加密 * @param data * @param key * @return * @throws Exception */ public static String encrypt(String data,String key) throws Exception { SecureRandom sr = new SecureRandom(); Key securekey = getSecretKey(key); Cipher cipher = Cipher.getInstance(TYPE); cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); byte[] bt = cipher.doFinal(data.getBytes()); String strs = Base64.getEncoder().encodeToString(bt); return strs; } /** * 文本解密 * @param message * @param key * @return * @throws Exception */ public static String detrypt(String message,String key) throws Exception{ SecureRandom sr = new SecureRandom(); Cipher cipher = Cipher.getInstance(TYPE); Key securekey = getSecretKey(key); cipher.init(Cipher.DECRYPT_MODE, securekey,sr); byte[] res = Base64.getDecoder().decode(message); res = cipher.doFinal(res); return new String(res); } }
相关推荐
JS(javascript)-md5加密工具类
SmUtil java实现sm2加密工具类 // 获取一条SM2曲线参数 X9ECParameters sm2ECParameters = GMNamedCurves.getByName("sm2p256v1"); // 构造ECC算法参数,曲线方程、椭圆曲线G点、大整数N ECDomainParameters ...
SHA1(加密工具类),已通过测试.
des加密工具类,des加密工具类,des加密工具类,des加密工具类,des加密工具类,
JavaMD5加密工具类 algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); algorithm.update(s.getBytes("UTF-8")); byte[] messageDigest = algorithm.digest(); return messageDigest;
java语言实现的RSA加密工具类
MD5加密工具类,MD5加密工具类,MD5加密工具类,MD5加密工具类,MD5加密工具类,MD5加密工具类,MD5加密工具类,MD5加密工具类
3des加密工具类
Java Md5加密工具类
AES加密解密工具类,解决windows下和linux加密解密不同问题。
js的md5加密工具类
JAVA AES对称加密工具类,可实用;包含加密字符串和文件流
java加密工具类。提供各种加密方法,加密方法有:aes,des,md5,sha等加密方式
des是java开发中比较常见的加密方式,多用于加密要求不是很严格的地方,...将加密方式封装成一个工具类,便于使用,可指定密钥或使用默认密钥,可指定字符或使用默认字符,用法简单灵活,全文中文注释,易于读和修改。
非常实用的rsa加解密工具类,加签验签,拿过去就能用,包含测试代码
解压缩加密工具类SSSS