package org.apereo.cas.web.landtool.utils;
|
|
|
import java.security.MessageDigest;
|
import java.util.Base64;
|
|
import javax.crypto.KeyGenerator;
|
import javax.crypto.Mac;
|
import javax.crypto.SecretKey;
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
//import sun.misc.BASE64Decoder;
|
//import sun.misc.BASE64Encoder;
|
|
/**
|
* 基础加密组件
|
* @author TanBin
|
*/
|
public abstract class CoderUtils {
|
public static final String KEY_SHA = "SHA";
|
public static final String KEY_MD5 = "MD5";
|
|
/**
|
* MAC算法可选以下多种算法
|
* <pre>
|
* HmacMD5
|
* HmacSHA1
|
* HmacSHA256
|
* HmacSHA384
|
* HmacSHA512
|
* </pre>
|
*/
|
public static final String KEY_MAC = "HmacMD5";
|
|
/**
|
* BASE64加密
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static String encryptBASE64(byte[] data) {
|
//text.getBytes("UTF-8"); new String(decoder.decode(encodedText), "UTF-8")
|
//法1:使用sun.misc套件。1.6之前, 效率不好,新JDK不支持
|
// return (new BASE64Encoder()).encodeBuffer(data);
|
|
//法2:使用Apache Commons Codec。
|
// return (new Base64()).encodeToString(data);
|
|
//法3:使用Java 8的java.util套件。 效率是1的11倍,是2的3倍以上。
|
return Base64.getEncoder().encodeToString(data);
|
}
|
|
/**
|
* BASE64解密
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] decryptBASE64(String key) {
|
//法1:使用sun.misc套件。 效率不好,新JDK不支持
|
// return (new BASE64Decoder()).decodeBuffer(key);
|
|
//法2:使用Apache Commons Codec。
|
// return (new Base64()).decode(key);
|
|
//法3:使用Java 8的java.util套件。 效率是1的11倍,是2的3倍以上。
|
return Base64.getDecoder().decode(key);
|
}
|
|
/**
|
* MD5加密
|
* @param data
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] encryptMD5(byte[] data) throws Exception{
|
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
|
md5.update(data);
|
return md5.digest();
|
}
|
|
/**
|
* 蓝图的MD5加密算法
|
* @param text
|
* @return
|
* @throws Exception
|
*/
|
public static String lantuEncryptMD5(String text) throws Exception{
|
byte[] b = encryptMD5(text.getBytes("UTF-8"));
|
String key = convertToHexString(b);
|
//转为大写
|
key = key.toUpperCase();
|
//倒序
|
char[] array = key.toCharArray();
|
String reverseKey = "";
|
for (int i = array.length-1; i >= 0; i--) {
|
reverseKey += array[i];
|
}
|
return reverseKey;
|
}
|
|
/**
|
* 字节数组转为32位字符串
|
* @param data
|
* @return
|
*/
|
private static String convertToHexString(byte data[]) {
|
StringBuffer strBuffer = new StringBuffer();
|
for (int i = 0; i < data.length; i++) {
|
//strBuffer.append(Integer.toHexString(0xff & data[i])); //30bit
|
String haxHex = Integer.toHexString(data[i] & 0xFF);
|
if(haxHex.length() < 2){
|
strBuffer.append("0");
|
}
|
strBuffer.append(haxHex);
|
}
|
return strBuffer.toString();
|
}
|
|
/**
|
* SHA加密
|
* @param data
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] encryptSHA(byte[] data) throws Exception{
|
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
|
sha.update(data);
|
return sha.digest();
|
}
|
|
/**
|
* 初始化HMAC密钥
|
* @return
|
* @throws Exception
|
*/
|
public static String initMacKey() throws Exception{
|
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
|
SecretKey secretKey = keyGenerator.generateKey();
|
return encryptBASE64(secretKey.getEncoded());
|
}
|
|
/**
|
* HMAC加密
|
* @param data
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] encryptHMAC(byte[] data, String key) throws Exception{
|
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
|
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
|
mac.init(secretKey);
|
return mac.doFinal(data);
|
}
|
|
public static void main(String[] args) {
|
String inputStr = "123456";
|
System.err.println("原文:" + inputStr);
|
try {
|
String codeBASE64 = encryptBASE64(inputStr.getBytes("UTF-8"));
|
String codeMD5 = lantuEncryptMD5(inputStr);
|
byte[] codeSHA = encryptSHA(inputStr.getBytes("UTF-8"));
|
|
String key = initMacKey();
|
System.err.println("Mac密钥:" + key);
|
byte[] codeHMAC = encryptHMAC(inputStr.getBytes("UTF-8"), key);
|
|
System.err.println("\nBASE64:" + codeBASE64);
|
System.err.println("BASE64解密:" + new String(decryptBASE64(codeBASE64),"UTF-8"));
|
System.err.println("MD5:" + codeMD5);
|
System.err.println("SHA:" + codeSHA);
|
System.err.println("HMAC:" + codeHMAC);
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
}
|