MD5.java
package util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5 { public static String encrypt(String s) { StringBuffer buf = new StringBuffer(); try { MessageDigest md5 = MessageDigest.getInstance("MD5"); md5.update(s.getBytes()); byte[] bytes = md5.digest(); for (int i = 0; i < bytes.length; i++) { int num = bytes[i]; if (num < 0) { num += 256; }
// 如果小于16,则加0补齐16进制两位数 if (num < 16) { buf.append(0); } buf.append(Integer.toHexString(num)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return buf.toString(); } public static void main(String[] args) { String password = "HELLOhi"; System.out.println(encrypt(password)); } }