基于Java语言的MD5加密Base64转换方法

提供了基于MD5加密16位和32位的方法

  1 import java.io.IOException;
  2 import java.math.BigInteger;
  3 import java.security.MessageDigest;
  4 import java.security.NoSuchAlgorithmException;
  5 import sun.misc.BASE64Decoder;
  6 import sun.misc.BASE64Encoder;
  7 /**
  8  * <p>标题: 编码工具类</p>
  9  * <p>功能: 对数据进行编码转换</p>
 10  * 作者:赵力
 11  */
 12 public class EncodeUtil
 13 {
 14     public static void main(String[] args) throws Exception
 15     {
 16         System.out.println(md5Encrypt16("需要进行MD5加密的字符串"));
 17     }
 18 
 19     /**
 20      * MD5加密16位
 21      * @param encryptStr 要加密数据
 22      * @return 返回16位加密结果
 23      * ZhaoLi
 24      */
 25     public static String md5Encrypt16(String encryptStr)
 26     {
 27         return md5Encrypt32(encryptStr).substring(8, 24);
 28     }
 29 
 30     /**
 31      * MD5加密32位
 32      * @param encryptStr 要加密数据
 33      * @return 32位加密结果
 34      * ZhaoLi
 35      */
 36     public static String md5Encrypt32(String encryptStr)
 37     {
 38         MessageDigest md5;
 39         try
 40         {
 41             md5 = MessageDigest.getInstance("MD5");
 42             byte[] md5Bytes = md5.digest(encryptStr.getBytes());
 43             StringBuffer hexValue = new StringBuffer();
 44             for (int i = 0; i < md5Bytes.length; i++)
 45             {
 46                 int val = (md5Bytes[i]) & 0xff;
 47                 if (val < 16)
 48                 {
 49                     hexValue.append("0");
 50                 }
 51                 hexValue.append(Integer.toHexString(val));
 52             }
 53             return hexValue.toString().toLowerCase();
 54         } catch (Exception e)
 55         {
 56             throw new RuntimeException(e);
 57         }
 58     }
 59 
 60     /** 
 61      * 结合base64实现md5加密 
 62      * @param msg 待加密字符串 
 63      * @return 获取md5后转为base64 
 64      * @throws Exception 
 65      */
 66     public static String md5EncryptBase64(String msg) throws Exception
 67     {
 68         return msg == null ? null : base64Encode(md5(msg));
 69     }
 70 
 71     /** 
 72      * 将byte[]转为各种进制的字符串 
 73      * @param bytes byte[] 
 74      * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制 
 75      * @return 转换后的字符串 
 76      */
 77     public static String binary(byte[] bytes, int radix)
 78     {
 79         return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数  
 80     }
 81 
 82     /** 
 83      * base 64 encode 
 84      * @param bytes 待编码的byte[] 
 85      * @return 编码后的base 64 code 
 86      */
 87     public static String base64Encode(byte[] bytes)
 88     {
 89         return new BASE64Encoder().encode(bytes);
 90     }
 91 
 92     /** 
 93      * base 64 decode 
 94      * @param base64Code 待解码的base 64 code 
 95      * @return 解码后的byte[] 
 96      * @throws Exception 
 97      */
 98     public static byte[] base64Decode(String base64Code)
 99     {
100         try
101         {
102             return base64Code == null ? null : new BASE64Decoder().decodeBuffer(base64Code);
103         } catch (IOException e)
104         {
105             throw new RuntimeException("报错内容", e);
106         }
107     }
108 
109     /** 
110      * 获取byte[]的md5值 
111      * @param bytes byte[] 
112      * @return md5 
113      * @throws Exception 
114      */
115     public static byte[] md5(byte[] bytes)
116     {
117         MessageDigest md;
118         try
119         {
120             md = MessageDigest.getInstance("MD5");
121         } catch (NoSuchAlgorithmException e)
122         {
123             throw new RuntimeException("报错内容", e);
124         }
125         md.update(bytes);
126         return md.digest();
127     }
128 
129     /** 
130      * 获取字符串md5值 
131      * @param msg  
132      * @return md5 
133      * @throws Exception 
134      */
135     public static byte[] md5(String msg)
136     {
137         return msg == null ? null : md5(msg.getBytes());
138     }
139 }

 

posted @ 2017-09-12 11:33  随风奔跑的狼  阅读(1876)  评论(0编辑  收藏  举报