import java.math.BigInteger;
import java.security.MessageDigest;
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;
import com.fnic.cdnmgr.sdk.comm.CDNException;
import com.fnic.cdnmgr.sdk.comm.CommUtils;
/**
* @Description:
* @author: weijingbo
* @CreateDate: 2012-10-9
* @version: V1.0
*/
public final class EncryptUtils
{
/**
* SHA算法
*/
private static final String KEY_SHA = "SHA";
/**
* MD5算法
*/
private static final String KEY_MD5 = "MD5";
/**
* MAC算法可选以下多种算法
*
* HmacMD5 HmacSHA1 HmacSHA256 HmacSHA384 HmacSHA512
*/
private static final String KEY_MAC = "HmacMD5";
/**
* 编码格式
*/
private static final String ENCODING = "UTF-8";
/**
* 加密次数
*/
private static final int ENCODETIMES = 100;
/**
* 加密
*
* @param key 名文
* @return 密文
* @throws CDNException 异常信息
*/
public static String encode(final String key) throws CDNException
{
if (CommUtils.isInValid(key))
{
throw new CDNException("key is null");
}
String strBase64 = "";
try
{
strBase64 = encryptBASE64(key.getBytes(ENCODING));
}
catch (Exception e)
{
throw new CDNException("encode error", e);
}
return CaesarAlgorithm.encrypt(strBase64);
}
/**
* 解密
*
* @param key 密文
* @return 名文
* @throws CDNException 异常信息
*/
public static String decode(final String key) throws CDNException
{
if (CommUtils.isInValid(key))
{
throw new CDNException("key is null");
}
String strBase64 = CaesarAlgorithm.decrypt(key);
String strResult = "";
try
{
byte[] decodeKey = decryptBASE64(strBase64);
strResult = new String(decodeKey, ENCODING);
}
catch (Exception e)
{
throw new CDNException("decode error", e);
}
return strResult;
}
/**
* 不可逆加密
*
* @param key 明文
* @return 密文
* @throws CDNException 异常信息
*/
public static String encodeIrreversible(final String key) throws CDNException
{
String strResult = "";
try
{
byte[] bResult = encode(key).getBytes(ENCODING);
for (int i = 0; i <= ENCODETIMES; ++i)
{
bResult = encryptMD5(bResult);
}
strResult = new BigInteger(bResult).toString(32);
}
catch (Exception e)
{
throw new CDNException("encode error", e);
}
return strResult;
}
/**
* BASE64解密
*
* @param key
* @return
* @throws Exception
*/
private static byte[] decryptBASE64(final String key) throws Exception
{
return (new BASE64Decoder()).decodeBuffer(key);
}
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
private static String encryptBASE64(final byte[] key) throws Exception
{
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
private static byte[] encryptMD5(final byte[] data) throws Exception
{
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
private static byte[] encryptSHA(final byte[] data) throws Exception
{
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
/**
* 初始化HMAC密钥
*
* @return
* @throws Exception
*/
private 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
*/
private static byte[] encryptHMAC(final byte[] data, final 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);
}
private static class CaesarAlgorithm
{
private static final char[] UC_ENCRYPT_CHARS = { 'D', 'X', 'U', 'P', 'I', 'B', 'M', 'J', 'C', 'E', 'T', 'N',
'K', 'O', 'G', 'W', 'R', 'S', 'F', 'Y', 'V', 'L', 'Z', 'Q', 'A', 'H' };
private static final char[] LC_ENCRYPT_CHARS = { 'd', 'x', 'u', 'p', 'i', 'b', 'm', 'j', 'c', 'e', 't', 'n',
'k', 'o', 'g', 'w', 'r', 's', 'f', 'y', 'v', 'l', 'z', 'q', 'a', 'h' };
private static char[] UC_DECRYPT_CHARS = new char[26];
private static char[] LC_DECRYPT_CHARS = new char[26];
static
{
for (int i = 0; i < 26; i++)
{
char b = UC_ENCRYPT_CHARS[i];
UC_DECRYPT_CHARS[b - 'A'] = (char) ('A' + i);
b = LC_ENCRYPT_CHARS[i];
LC_DECRYPT_CHARS[b - 'a'] = (char) ('a' + i);
}
}
public static char encrypt(final char b)
{
if ((b >= 'A') && (b <= 'Z'))
{
return UC_ENCRYPT_CHARS[b - 'A'];
}
else if ((b >= 'a') && (b <= 'z'))
{
return LC_ENCRYPT_CHARS[b - 'a'];
}
else
{
return b;
}
}
public static char decrypt(final char b)
{
if ((b >= 'A') && (b <= 'Z'))
{
return UC_DECRYPT_CHARS[b - 'A'];
}
else if ((b >= 'a') && (b <= 'z'))
{
return LC_DECRYPT_CHARS[b - 'a'];
}
else
{
return b;
}
}
public static String encrypt(final String input)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++)
{
sb.append(encrypt(input.charAt(i)));
}
return sb.toString().trim();
}
public static String decrypt(final String input)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++)
{
sb.append(decrypt(input.charAt(i)));
}
return sb.toString();
}
}
public static void main(final String[] args)
{
String inputStr = "Admin123";
System.out.println("原文: " + inputStr);
byte[] inputData = inputStr.getBytes();
try
{
String code = EncryptUtils.encode(inputStr);
System.out.println("BASE64+凯撒加密后: " + code);
System.out.println("BASE64+凯撒解密后: " + EncryptUtils.decode(code));
System.out.println("不可逆加密后" + "(" + ENCODETIMES + " times): " + EncryptUtils.encodeIrreversible(code));
String key = EncryptUtils.initMacKey();
System.out.println("Mac密钥: " + key);
BigInteger md5 = new BigInteger(EncryptUtils.encryptMD5(inputData));
System.out.println("MD5: " + md5.toString(16));
BigInteger sha = new BigInteger(EncryptUtils.encryptSHA(inputData));
System.out.println("SHA: " + sha.toString(32));
BigInteger mac = new BigInteger(EncryptUtils.encryptHMAC(inputData, inputStr));
System.out.println("HMAC: " + mac.toString(16));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}