Java???????о?
???????????? ???????[ 2014/7/18 14:55:32 ] ????????Java ???????
???????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??????????????????????????????????????á?
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11