MD5加密(java代码)

package hdty.project.test.util; 

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 

public class EncoderByMd5 { 

public static String encodeByMd5(String plainText) { 
StringBuffer buf = new StringBuffer(""); 
try { 
MessageDigest md = MessageDigest.getInstance("MD5"); 
md.update(plainText.getBytes()); 
byte b[] = md.digest(); 
int i = 0; 
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)); 
} 

} catch (NoSuchAlgorithmException e) { 
e.printStackTrace(); 
} 
return buf.toString(); //32位的加密 
//return buf.toString().substring(8,24); // 16位的加密 
} 

public static void main(String[] args) throws NoSuchAlgorithmException, 
UnsupportedEncodingException { 
System.out.println(Md5("6")); 

} 
} 

  .

posted @ 2019-11-15 00:18  图图小淘气_real  阅读(781)  评论(0编辑  收藏  举报