使用java自带加密算法实现文本的md5加密算法

来源:互联网 发布:基金套利软件 编辑:程序博客网 时间:2024/05/22 08:02

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/21456943

       本篇使用Java自带的MessageDigest实现对文本的md5加密算法,具体代码如下:

[java] view plain copy
 print?
  1.  /**   
  2.  *@Description: 将字符串转化为MD5 
  3.  */   
  4. package cn.yicha.novel.util;    
  5.   
  6. import java.security.MessageDigest;  
  7. import java.security.NoSuchAlgorithmException;  
  8.     
  9. public class ParseMD5 {  
  10.   
  11.     /** 
  12.      * @param str 
  13.      * @return 
  14.      * @Date: 2013-9-6   
  15.      * @Author: lulei   
  16.      * @Description:  32位小写MD5 
  17.      */  
  18.     public static String parseStrToMd5L32(String str){  
  19.         String reStr = null;  
  20.         try {  
  21.             MessageDigest md5 = MessageDigest.getInstance("MD5");  
  22.             byte[] bytes = md5.digest(str.getBytes());  
  23.             StringBuffer stringBuffer = new StringBuffer();  
  24.             for (byte b : bytes){  
  25.                 int bt = b&0xff;  
  26.                 if (bt < 16){  
  27.                     stringBuffer.append(0);  
  28.                 }   
  29.                 stringBuffer.append(Integer.toHexString(bt));  
  30.             }  
  31.             reStr = stringBuffer.toString();  
  32.         } catch (NoSuchAlgorithmException e) {  
  33.             e.printStackTrace();  
  34.         }  
  35.         return reStr;  
  36.     }  
  37.       
  38.     /** 
  39.      * @param str 
  40.      * @return 
  41.      * @Date: 2013-9-6   
  42.      * @Author: lulei   
  43.      * @Description: 32位大写MD5 
  44.      */  
  45.     public static String parseStrToMd5U32(String str){  
  46.         String reStr = parseStrToMd5L32(str);  
  47.         if (reStr != null){  
  48.             reStr = reStr.toUpperCase();  
  49.         }  
  50.         return reStr;  
  51.     }  
  52.       
  53.     /** 
  54.      * @param str 
  55.      * @return 
  56.      * @Date: 2013-9-6   
  57.      * @Author: lulei   
  58.      * @Description: 16位小写MD5 
  59.      */  
  60.     public static String parseStrToMd5U16(String str){  
  61.         String reStr = parseStrToMd5L32(str);  
  62.         if (reStr != null){  
  63.             reStr = reStr.toUpperCase().substring(824);  
  64.         }  
  65.         return reStr;  
  66.     }  
  67.       
  68.     /** 
  69.      * @param str 
  70.      * @return 
  71.      * @Date: 2013-9-6   
  72.      * @Author: lulei   
  73.      * @Description: 16位大写MD5 
  74.      */  
  75.     public static String parseStrToMd5L16(String str){  
  76.         String reStr = parseStrToMd5L32(str);  
  77.         if (reStr != null){  
  78.             reStr = reStr.substring(824);  
  79.         }  
  80.         return reStr;  
  81.     }  
  82. }  
原创粉丝点击