Base64编解码
/**
* Base64编码.
*/
public static String encodeBase64(byte[] input) {
return new String(Base64.encodeBase64(input));
}
/**
* Base64编码.
*/
public static String encodeBase64(String input) {
try {
return new String(Base64.encodeBase64(input.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* Base64解码.
*/
public static byte[] decodeBase64(String input) {
return Base64.decodeBase64(input.getBytes());
}
/**
* Base64解码.
*/
public static String decodeBase64String(String input) {
try {
return new String(Base64.decodeBase64(input.getBytes()), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
URL 编解码
/**
* URL 编码, Encode默认为UTF-8.
*/
public static String urlEncode(String part) {
try {
return URLEncoder.encode(part, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Exceptions.unchecked(e);
}
}
/**
* URL 解码, Encode默认为UTF-8.
*/
public static String urlDecode(String part) {
try {
return URLDecoder.decode(part, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw Exceptions.unchecked(e);
}
}
sha56_Hmac加密
/**
* sha56_Hmac加密
* @param message
* @param secret
* @return
* @throws Exception
*/
public static String sha256_HMAC(String message, String secret) throws Exception{
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
mac.init(keySpec);
byte[] bs = mac.doFinal(message.getBytes());
return byte2Hex(bs);
}
java自带类实现SHA-256方式加密
/**
* java自带类实现SHA-256方式加密
* @param str
* @return
*/
public static String getSHA256StrJava(String str){
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}
MD5对字符串进行加密
/**
* MD5对字符串进行加密
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public static String getMD5Str(String str) throws NoSuchAlgorithmException{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(str.getBytes());
return byte2Hex(bytes).toUpperCase();
}