版权声明:本文为博主原创文章,未经博主允许不得转载。

【md5】

md5是一种哈希算法,哈希算法是啥? 。。。

特点是不能解密。


【代码】

[java] view plain copy Java 加密 MD5_javaJava 加密 MD5_java_02
  1. package com.uikoo9.util.encrypt;  
  2.   
  3. import java.math.BigInteger;  
  4. import java.security.MessageDigest;  
  5.   
  6. import sun.misc.BASE64Decoder;  
  7. import sun.misc.BASE64Encoder;  
  8.   
  9. import com.uikoo9.util.QStringUtil;  
  10.   
  11. /** 
  12.  * 编码工具类 
  13.  * 1.将byte[]转为各种进制的字符串 
  14.  * 2.base 64 encode 
  15.  * 3.base 64 decode 
  16.  * 4.获取byte[]的md5值 
  17.  * 5.获取字符串md5值 
  18.  * 6.结合base64实现md5加密 
  19.  * @author uikoo9 
  20.  * @version 0.0.6.20140601 
  21.  */  
  22. public class QEncodeUtil {  
  23.       
  24.     public static void main(String[] args) throws Exception {  
  25.         String msg = "我爱你";  
  26.         System.out.println("加密前:" + msg);  
  27.           
  28.         String encrypt = md5Encrypt(msg);  
  29.         System.out.println("加密后:" + encrypt);  
  30.     }  
  31.       
  32.     /** 
  33.      * 将byte[]转为各种进制的字符串 
  34.      * @param bytes byte[] 
  35.      * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制 
  36.      * @return 转换后的字符串 
  37.      */  
  38.     public static String binary(byte[] bytes, int radix){  
  39.         return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数  
  40.     }  
  41.       
  42.     /** 
  43.      * base 64 encode 
  44.      * @param bytes 待编码的byte[] 
  45.      * @return 编码后的base 64 code 
  46.      */  
  47.     public static String base64Encode(byte[] bytes){  
  48.         return new BASE64Encoder().encode(bytes);  
  49.     }  
  50.       
  51.     /** 
  52.      * base 64 decode 
  53.      * @param base64Code 待解码的base 64 code 
  54.      * @return 解码后的byte[] 
  55.      * @throws Exception 
  56.      */  
  57.     public static byte[] base64Decode(String base64Code) throws Exception{  
  58.         return QStringUtil.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
  59.     }  
  60.       
  61.     /** 
  62.      * 获取byte[]的md5值 
  63.      * @param bytes byte[] 
  64.      * @return md5 
  65.      * @throws Exception 
  66.      */  
  67.     public static byte[] md5(byte[] bytes) throws Exception {  
  68.         MessageDigest md = MessageDigest.getInstance("MD5");  
  69.         md.update(bytes);  
  70.           
  71.         return md.digest();  
  72.     }  
  73.       
  74.     /** 
  75.      * 获取字符串md5值 
  76.      * @param msg  
  77.      * @return md5 
  78.      * @throws Exception 
  79.      */  
  80.     public static byte[] md5(String msg) throws Exception {  
  81.         return QStringUtil.isEmpty(msg) ? null : md5(msg.getBytes());  
  82.     }  
  83.       
  84.     /** 
  85.      * 结合base64实现md5加密 
  86.      * @param msg 待加密字符串 
  87.      * @return 获取md5后转为base64 
  88.      * @throws Exception 
  89.      */  
  90.     public static String md5Encrypt(String msg) throws Exception{  
  91.         return QStringUtil.isEmpty(msg) ? null : base64Encode(md5(msg));  
  92.     }  
  93.       
  94. }  


【输出】

[java] view plain copy Java 加密 MD5_javaJava 加密 MD5_java_02
    1. 加密前:我爱你  
    2. 加密后:TyAWxrk01VvXEg5dDmLM4w==