Created
October 21, 2012 08:16
-
-
Save binjoo/3926300 to your computer and use it in GitHub Desktop.
JAVA:MD5加密
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class Md5 { | |
public static String toMd5(String arg) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(arg.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)); | |
} | |
return buf.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static void main(String[] args) { | |
System.out.println(Md5.toMd5("123")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment