java md5编码

发现很多数据库的md5是16位,而且是取了32位中间16位
Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20util%3B%0A%0Aimport%20java.security.MessageDigest%3B%0Aimport%20java.security.NoSuchAlgorithmException%3B%0A%0A%2F**%0A%20*%20%E8%BF%99%E9%87%8CMD5_16%E5%8F%96%E4%BA%86MD5_32%E7%9A%84%E4%B8%AD%E9%97%B416%E4%BD%8D%0A%20*%20%0A%20*%20%40author%20hehui%0A%20*%20%0A%20*%2F%0Apublic%20class%20MD5%20%7B%0A%0A%09private%20static%20String%20byte2Hex(byte%20b)%20%7B%0A%09%09int%20value%20%3D%20(b%20%26%200x7F)%20%2B%20(b%20%3C%200%20%3F%200x80%20%3A%200)%3B%0A%09%09return%20(value%20%3C%200x10%20%3F%20%220%22%20%3A%20%22%22)%0A%09%09%09%09%2B%20Integer.toHexString(value).toLowerCase()%3B%0A%09%7D%0A%0A%09public%20static%20String%20MD5_32(String%20passwd)%20throws%20NoSuchAlgorithmException%20%7B%0A%09%09MessageDigest%20md5%20%3D%20MessageDigest.getInstance(%22MD5%22)%3B%0A%09%09StringBuffer%20strbuf%20%3D%20new%20StringBuffer()%3B%0A%0A%09%09md5.update(passwd.getBytes()%2C%200%2C%20passwd.length())%3B%0A%09%09byte%5B%5D%20digest%20%3D%20md5.digest()%3B%0A%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%20digest.length%3B%20i%2B%2B)%20%7B%0A%09%09%09strbuf.append(byte2Hex(digest%5Bi%5D))%3B%0A%09%09%7D%0A%0A%09%09return%20strbuf.toString()%3B%0A%09%7D%0A%0A%09public%20static%20String%20MD5_16(String%20passwd)%20throws%20NoSuchAlgorithmException%20%7B%0A%09%09return%20MD5_32(passwd).subSequence(8%2C%2024).toString()%3B%0A%09%7D%0A%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%09%09try%20%7B%0A%09%09%09System.out.println(MD5_32(%22www.i123.cn%22))%3B%0A%09%09%09%0A%09%09%09System.out.println(MD5_16(%22www.i123.cn%22))%3B%0A%09%09%7D%20catch%20(NoSuchAlgorithmException%20e)%20%7B%0A%09%09%09%2F%2F%20TODO%20Auto-generated%20catch%20block%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%0A%09%7D%0A%7D%0A" src="http://hehuiahui.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1233719171222" lk_media="yes">
  1. package util;  
  2.   
  3. import java.security.MessageDigest;  
  4. import java.security.NoSuchAlgorithmException;  
  5.   
  6. /**
  7. * 这里MD5_16取了MD5_32的中间16位
  8. *
  9. * @author hehui
  10. *
  11. */  
  12. public class MD5 {  
  13.   
  14.     private static String byte2Hex(byte b) {  
  15.         int value = (b & 0x7F) + (b < 0 ? 0x80 : 0);  
  16.         return (value < 0x10 ? "0" : "")  
  17.                  + Integer.toHexString(value).toLowerCase();  
  18.      }  
  19.   
  20.     public static String MD5_32(String passwd) throws NoSuchAlgorithmException {  
  21.          MessageDigest md5 = MessageDigest.getInstance("MD5");  
  22.          StringBuffer strbuf = new StringBuffer();  
  23.   
  24.          md5.update(passwd.getBytes(), 0, passwd.length());  
  25.         byte[] digest = md5.digest();  
  26.   
  27.         for (int i = 0; i < digest.length; i++) {  
  28.              strbuf.append(byte2Hex(digest[i]));  
  29.          }  
  30.   
  31.         return strbuf.toString();  
  32.      }  
  33.   
  34.     public static String MD5_16(String passwd) throws NoSuchAlgorithmException {  
  35.         return MD5_32(passwd).subSequence(8, 24).toString();  
  36.      }  
  37.   
  38. public static void main(String[] args) {  
  39.         try {  
  40.              System.out.println(MD5_32("www.i123.cn"));  
  41.               
  42.              System.out.println(MD5_16("www.i123.cn"));  
  43.          } catch (NoSuchAlgorithmException e) {  
  44.             // TODO Auto-generated catch block  
  45.              e.printStackTrace();  
  46.          }  
  47.      }  
  48. }

你可能感兴趣的:(java)