Skip to content

Instantly share code, notes, and snippets.

@shautzin
Created May 24, 2014 05:08
Show Gist options
  • Save shautzin/ba542e42781a14f86952 to your computer and use it in GitHub Desktop.
Save shautzin/ba542e42781a14f86952 to your computer and use it in GitHub Desktop.
package com.liuxey.uitl;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.SecureRandom;
/**
* <p>加密工具类
*
* @author Liuxey
* @date 2014年1月14日
*/
public class Crypto {
// AES
/**
* <p>
* // Description AES 加密
*
* @param content 加密内容
* @param salt 加密密钥
* @param encoding 编码, 如:UTF-8
*
* @return 加密串
*/
public static String AESEncrypt(String content, String salt, String encoding) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(salt.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES"); // 创建密码器
byte[] byteContent = content.getBytes(encoding);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return parseByte2HexStr(result);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* // Description AES 解密
*
* @param code 加密串
* @param salt 加密密钥
* @param encoding 编码, 如:UTF-8
*
* @return 原始内容
*/
public static String AESDecrypt(String code, String salt, String encoding) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(salt.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES"); // 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(parseHexStr2Byte(code));
return new String(result, encoding);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* <p>
* // Description AES 加密
*
* @param content 加密内容
* @param salt 加密密钥
*
* @return 加密串
*/
public static String AESEncrypt(String content, String salt) {
return AESEncrypt(content, salt, Constant.ENCODING);
}
/**
* <p>
* // Description AES 解密
*
* @param code 加密串
* @param salt 加密密钥
*
* @return 原始内容
*/
public static String AESDecrypt(String code, String salt) {
return AESDecrypt(code, salt, Constant.ENCODING);
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
private 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进制转换为二进制
*
* @param hexStr
* @return
*/
private 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 bytes
* @return
*/
public final static String md5(byte[] bytes) {
try {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(bytes);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
/**
* <p> MD5 加密
*
* @param content 加密内容
* @param encoding 编码, 如:UTF-8
*
* @return 加密串
*/
public final static String md5(String content, String encoding) {
try {
byte[] strTemp = content.getBytes(encoding);
return md5(strTemp);
} catch (Exception e) {
return null;
}
}
/**
* <p> MD5 加密
*
* @param content 加密内容
*
* @return 加密串
*/
public final static String md5(String content) {
return md5(content, Constant.ENCODING);
}
/**
* 加盐 MD5 加密
* @param content
* @return
*/
public final static String md5WithSalt(String content, String salt) {
return md5WithSalt(content, salt, Constant.ENCODING);
}
/**
* 加盐 MD5 加密
*
* @param content
* @param salt
* @param encoding
* @return
*/
public final static String md5WithSalt(String content, String salt, String encoding) {
return md5(content + "#" + salt, encoding);
}
/**
* <p> URL编码
*
* @param content 编码内容
* @param encode 编码
* @return
*/
public final static String urlEncode(String content, String encode) {
if (content != null && !content.equals("")) {
try {
String result = URLEncoder.encode(content, encode);
return result;
} catch (UnsupportedEncodingException e) {
return "";
}
}else {
return "";
}
}
/**
* <p> URL解码
*
* @param content 编码内容
* @param encode 编码
* @return
*/
public final static String urlDecode(String content, String encode) {
if (content != null && !content.equals("")) {
try {
String result = URLDecoder.decode(content, encode);
return result;
} catch (UnsupportedEncodingException e) {
return "";
}
}else {
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment