生成32位加密文:
public static String md5Encode(String inStr) throws Exception {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes("UTF-8");
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
生成16位加密文:
public static String md5Encode(String inStr) throws Exception {
String result = "";
// 通过result返回结果值
try {
MessageDigest md = MessageDigest.getInstance("MD5");
// 1.初始化MessageDigest信息摘要对象,并指定为MD5不分大小写都可以
md.update(Str.getBytes());
// 2.传入需要计算的字符串更新摘要信息,传入的为字节数组byte[],将字符串转换为字节数组使用getBytes()方法完成
byte b[] = md.digest();
// 3.计算信息摘要digest()方法,返回值为字节数组
int i;// 定义整型 //声明StringBuffer对象
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];// 将首个元素赋值给i
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");// 前面补0
buf.append(Integer.toHexString(i));// 转换成16进制编码
}
result = buf.toString().substring(8, 24);// 转换成字符串
} catch (Exception e) {
System.out.println(e);
}
eturn result;// 返回结果
}
解密操作(适用于上面两种情况,但是存在一定的弊端,要解密两次才可以):
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}