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);
}
}