package com.binsoft.util; import java.security.MessageDigest; public class Core { private String MD5; private String text; /** * * @param text * @return * @throws Exception */ public boolean setContent(String text) throws Exception { this.text = text; this.MD5 = getMD5(text); return true; } /** * * @return * @throws Exception */ public String getContent() throws Exception { return this.text; } /** * * @return * @throws Exception */ public String getMD5() throws Exception { return this.MD5; } /** * * @param data * @return * @throws Exception */ private String convertToHex(byte[] data) throws Exception { StringBuffer buf = new StringBuffer(); for (int i = 0; i < data.length; i++) { //除以16 int halfbyte = (data[i] >>> 4) & 0x0F; int two_halfs = 0; do { if ((0 <= halfbyte) && (halfbyte <= 9)) { buf.append((char) ('0' + halfbyte)); } else { buf.append((char) ('a' + (halfbyte - 10))); } halfbyte = data[i] & 0x0F; } while (two_halfs++ < 1); } return buf.toString(); } private String getMD5(String text) throws Exception{ MessageDigest md; md = MessageDigest.getInstance("MD5"); byte[] md5hash = new byte[32]; md.update(text.getBytes("utf-8"),0,text.length()); md5hash = md.digest(); return convertToHex(md5hash); } /** * @param args */ public static void main(String[] args) { Core core = new Core(); try { //E87DB0C7EEEA37AEDC9EEB92E2DA5340 core.setContent("libinjareo"); System.out.println("加密后的数值: " + core.MD5); } catch (Exception e) { e.printStackTrace(); } } }