zoukankan      html  css  js  c++  java
  • md加密 16位 32位

    16位大写

    //生成MD5
    public static String getMD5(String message) {
    String md5 = "";
    try {
    MessageDigest md = MessageDigest.getInstance("MD5"); // 创建一个md5算法对象
    byte[] messageByte = message.getBytes("UTF-8");
    byte[] md5Byte = md.digest(messageByte); // 获得MD5字节数组,16*8=128位
    md5 = bytesToHex(md5Byte); // 转换为16进制字符串
    } catch (Exception e) {
    e.printStackTrace();
    }
    return md5;
    }

    // 二进制转十六进制
    public static String bytesToHex(byte[] bytes) {
    StringBuffer hexStr = new StringBuffer();
    int num;
    for (int i = 0; i < bytes.length; i++) {
    num = bytes[i];
    if(num < 0) {
    num += 256;
    }
    if(num < 16){
    hexStr.append("0");
    }
    hexStr.append(Integer.toHexString(num));
    }
    return hexStr.toString().toUpperCase();
    }

    _______________________________________________________________________________

    //32位小写

    String sign = encryption(str);

    public static String encryption(String plain) {
    String re_md5 = new String();
    MessageDigest md;
    try {
    md = MessageDigest.getInstance("MD5");
    md.update(plain.getBytes());
    byte b[] = md.digest();
    int i;
    StringBuffer buf = new StringBuffer("");
    for (int offset = 0; offset < b.length; offset++) {
    i = b[offset];
    if (i < 0)
    i += 256;
    if (i < 16)
    buf.append("0");
    buf.append(Integer.toHexString(i));
    }
    re_md5 = buf.toString();
    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return re_md5;
    }

  • 相关阅读:
    1988-B. 有序集合
    1987-A. 集训队选拔
    1964-NP
    1963-带妹子去看电影
    1962-Fibonacci
    1961-计算机基础知识大赛 2 (new)
    TCP/IP协议详解 卷一:协议 18章、TCP连接的建立与终止
    3、剑指offer--从尾到头打印链表
    2、剑指offer--替换空格
    1、剑指offer--二维数组中查找
  • 原文地址:https://www.cnblogs.com/wudage/p/7644151.html
Copyright © 2011-2022 走看看