zoukankan      html  css  js  c++  java
  • Java 加密 MD5

    【md5】

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

    特点是不能解密。


    【代码】

    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. }  


    【输出】

      1. 加密前:我爱你  
      2. 加密后:TyAWxrk01VvXEg5dDmLM4w==
  • 相关阅读:
    cookie
    sql 语句
    页面宽高
    分页
    asp.net中如何防止用户重复点击提交按钮
    小试简单工厂模式之简单计算器
    用函数实现交换的疑问
    结构体变量输入输出的问题
    scanf函数输入float数需要注意的问题
    oracle学习手记(1)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5824635.html
Copyright © 2011-2022 走看看