MD5加密
public static String getMD5(String sourceStr)
throws UnsupportedEncodingException {
String resultStr = "";
try {
byte[] temp = sourceStr.getBytes();
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(temp);
byte[] b = md5.digest();
for (int i = 0; i < b.length; i++) {
char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
char[] ob = new char[2];
ob[0] = digit[(b[i] >>> 4) & 0X0F];
ob[1] = digit[b[i] & 0X0F];
resultStr += new String(ob);
}
return resultStr;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
URL编码加解密
public static void main(String[] args) {
try {
String str = URLEncoder.encode("aaaabbbbccc","utf-8");
System.out.println(URLDecoder.decode(str,"utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
}
DES3加解密
public static final String ENCRYPT_KEY = "********************";
public static String encodeToDes3(String str) {
return ServiceUtils.encodeToDes3(str, ServiceConstants.ENCRYPT_KEY);
}
public static String encodeToDes3(String str, String encryptKey) {
try {
if (StringUtils.isBlank(str)) {
return null;
}
return java.net.URLEncoder.encode(
EncryptionUtils.encrypt(encryptKey, str), "utf-8")
.replaceAll("\\+", "%2B");
} catch (Exception e) {
LOGGER.warn("des3加密失败:" + str);
return null;
}
}
public static String decodeFromDes3(String str, String encryptKey) {
try {
if (StringUtils.isBlank(str)) {
return null;
}
String tmp = null;
tmp = str.replace("+", "%2B");
tmp = tmp.replace("=", "%3D");
tmp = tmp.replace("%25", "%");
return EncryptionUtils.decrypt(encryptKey,
java.net.URLDecoder.decode(tmp, "utf-8"));
} catch (Exception e) {
LOGGER.warn("des3解密失败:" + str);
return null;
}
}
unicode解码
public static String unicodeDecode(String string) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(string);
char ch;
while (matcher.find()) {
ch = (char) Integer.parseInt(matcher.group(2), 16);
string = string.replace(matcher.group(1), ch + "");
}
return string;
}
获取UUID
public static void main(String[] args) {
String uid = UUID.randomUUID().toString();
System.out.println(uid);
}