md5加密 bas64加密

/**
* 获取bas64加密的算法
* @param str
* @return
*/
public static String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}

/**
* 获取32位MD5加密的算法
* @param sourceStr
* @return
*/
private static String MD5(String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
System.out.println("MD5(" + sourceStr + ",32) = " + result);
System.out.println("MD5(" + sourceStr + ",16) = " + buf.toString().substring(8, 24));
} catch (NoSuchAlgorithmException e) {
System.out.println(e);
}
return result;
}

上一篇:重新精读《Java 编程思想》系列之类的访问权限


下一篇:jboss内存查看管理 .