当前位置: 移动技术网 > IT编程>开发语言>Java > 基于Java语言MD5加密Base64转换方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文提供了基于md5加密16位和32位的方法,具体内容如下

import java.io.ioexception;
import java.math.biginteger;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
import sun.misc.base64decoder;
import sun.misc.base64encoder;
/**
 * <p>标题:编码工具类</p>
 * <p>功能:对数据进行编码转换</p>
 * 作者:赵力
 */
public class encodeutil
{
  public static void main(string[] args) throws exception
  {
    system.out.println(md5encrypt16("需要进行md5加密的字符串"));
  }

  /**
   * md5加密16位
   * @param encryptstr 要加密数据
   * @return 返回16位加密结果
   * zhaoli
   */
  public static string md5encrypt16(string encryptstr)
  {
    return md5encrypt32(encryptstr).substring(8, 24);
  }

  /**
   * md5加密32位
   * @param encryptstr 要加密数据
   * @return 32位加密结果
   * zhaoli
   */
  public static string md5encrypt32(string encryptstr)
  {
    messagedigest md5;
    try
    {
      md5 = messagedigest.getinstance("md5");
      byte[] md5bytes = md5.digest(encryptstr.getbytes());
      stringbuffer hexvalue = new stringbuffer();
      for (int i = 0; i < md5bytes.length; i++)
      {
        int val = (md5bytes[i]) & 0xff;
        if (val < 16)
        {
          hexvalue.append("0");
        }
        hexvalue.append(integer.tohexstring(val));
      }
      return hexvalue.tostring().tolowercase();
    } catch (exception e)
    {
      throw new runtimeexception(e);
    }
  }

  /** 
   * 结合base64实现md5加密 
   * @param msg 待加密字符串 
   * @return 获取md5后转为base64 
   * @throws exception 
   */
  public static string md5encryptbase64(string msg) throws exception
  {
    return msg == null ? null : base64encode(md5(msg));
  }

  /** 
   * 将byte[]转为各种进制的字符串 
   * @param bytes byte[] 
   * @param radix 可以转换进制的范围,从character.min_radix到character.max_radix,超出范围后变为10进制 
   * @return 转换后的字符串 
   */
  public static string binary(byte[] bytes, int radix)
  {
    return new biginteger(1, bytes).tostring(radix);// 这里的1代表正数 
  }

  /** 
   * base 64 encode 
   * @param bytes 待编码的byte[] 
   * @return 编码后的base 64 code 
   */
  public static string base64encode(byte[] bytes)
  {
    return new base64encoder().encode(bytes);
  }

  /** 
   * base 64 decode 
   * @param base64code 待解码的base 64 code 
   * @return 解码后的byte[] 
   * @throws exception 
   */
  public static byte[] base64decode(string base64code)
  {
    try
    {
      return base64code == null ? null : new base64decoder().decodebuffer(base64code);
    } catch (ioexception e)
    {
      throw new runtimeexception("报错内容", e);
    }
  }

  /** 
   * 获取byte[]的md5值 
   * @param bytes byte[] 
   * @return md5 
   * @throws exception 
   */
  public static byte[] md5(byte[] bytes)
  {
    messagedigest md;
    try
    {
      md = messagedigest.getinstance("md5");
    } catch (nosuchalgorithmexception e)
    {
      throw new runtimeexception("报错内容", e);
    }
    md.update(bytes);
    return md.digest();
  }

  /** 
   * 获取字符串md5值 
   * @param msg 
   * @return md5 
   * @throws exception 
   */
  public static byte[] md5(string msg)
  {
    return msg == null ? null : md5(msg.getbytes());
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网