1.javaä¸md5å å¯
2.javaå¦ä½ç®md5ç ï¼
javaä¸md5å å¯
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class md5 {
public String str;
public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.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 += ;
if (i < )
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// ä½çå å¯
System.out.println("result: " + buf.toString().substring(8,源码feap源码 ));// ä½çå å¯
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String agrs[]) {
md5 md = new md5();
md.md5s("4");//å å¯4
}
}
javaå¦ä½ç®md5ç ï¼
å¯ä»¥å©ç¨JDKèªå¸¦çMD5æ¥å å¯ãpublic class MD5Util {public final static String MD5(String s) {
char hexDigits[]={ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
try {
byte[] btInput = s.getBytes();
// è·å¾MD5æè¦ç®æ³ç MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使ç¨æå®çåèæ´æ°æè¦
mdInst.update(btInput);
// è·å¾å¯æ
byte[] md = mdInst.digest();
// æå¯æ转æ¢æåå è¿å¶çå符串形å¼
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(MD5Util.MD5(""));
System.out.println(MD5Util.MD5("å å¯"));
}
}