???????MD5????(Java???)
?????????????????????????????????????????譴???MD5????????Java???security?е?MessageDigest??????MD5????????????????????????????????Ч??????????????????MD5??????

 

import java.security.MessageDigest;
/**
* ???MD5????????????java????security????MessageDigest????
*/
public class MD5 {
/**
* ???MD5????????????
*/
public static String getMD5ofStr(String origString) {
String origMD5 = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
// md5.update(origString.getBytes());
byte[] result = md5.digest(origString.getBytes());
origMD5 = byteArray2HexStr(result);
// if ("123".equals(origString)) {
// System.out.println(new String(result));
// System.out.println(new BigInteger(result).toString(16));
// }
} catch (Exception e) {
e.printStackTrace();
}
return origMD5;
}
/**
* ?????????????MD5????????
*/
private static String byteArray2HexStr(byte[] bs) {
StringBuffer sb = new StringBuffer();
for (byte b : bs) {
sb.append(byte2HexStr(b));
}
return sb.toString();
}
/**
* ???????λ???????????
*/
private static String byte2HexStr(byte b) {
String hexStr = null;
int n = b;
if (n < 0) {
// ????????????????????????λ??????
n = b & 0x7F + 128;
}
hexStr = Integer.toHexString(n / 16) + Integer.toHexString(n % 16);
return hexStr.toUpperCase();
}
/**
* ?????MD5??μ??????
*/
public static String getMD5ofStr(String origString?? int times) {
String md5 = getMD5ofStr(origString);
for (int i = 0; i < times - 1; i++) {
md5 = getMD5ofStr(md5);
}
return getMD5ofStr(md5);
}
/**
* ???????????
*/
public static boolean verifyPassword(String inputStr?? String MD5Code) {
return getMD5ofStr(inputStr).equals(MD5Code);
}
/**
* ?????????μ?????????????????
*/
public static boolean verifyPassword(String inputStr?? String MD5Code??
int times) {
return getMD5ofStr(inputStr?? times).equals(MD5Code);
}
/**
* ????????????????
*/
public static void main(String[] args) {
System.out.println("123:" + getMD5ofStr("123"));
System.out.println("123456789:" + getMD5ofStr("123456789"));
System.out.println("sarin:" + getMD5ofStr("sarin"));
System.out.println("123:" + getMD5ofStr("123"?? 4));
}
}

?????????????????????????????java????????????????????????????????????????????????MD5?????????????λ????????????MD5???????????????????????????????????λ????????????????????λ?????????32λ??????????????????????????????????????????????????
????MD5???????????????CMD5??Щ???????????????MD5??????????е????????????λ??????????Ч?????????????????????MD5???????????
??????????????????μ???????????????MD5??????????MD5?????32λ?????μ???????MD5??????????????????κ????塣
??????????MIS??????????????????????????????????????á?