码迷,mamicode.com
首页 > 其他好文 > 详细

MD5 加密类使用

时间:2018-08-02 19:16:50      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:valueof   print   return   turn   ESS   string   res   int   stat   

/**
* Created by Administrator on 2017/6/2 0002.
*/

import java.io.IOException;
import java.math.RoundingMode;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.DecimalFormat;


public class MD5 {
private static final String[] hexDigits = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

public MD5() {
}

public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();

for (int i = 0; i < b.length; ++i) {
resultSb.append(byteToHexString(b[i]));
}

return resultSb.toString();
}

private static String byteToHexString(byte b) {
int n = b;
if (b < 0) {
n = b + 256;
}

int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}

public static String MD5Encode(String origin) {
if (origin == null) {
return null;
} else {
String resultString = "";

try {
resultString = new String(origin);
MessageDigest ex = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(ex.digest(resultString.getBytes()));
} catch (Exception var3) {
System.out.println("MD5 encode error:" + var3.getMessage());
}

return resultString;
}
}

public static String MD5Encode(byte[] b) {
if (b == null) {
return null;
} else {
String resultString = "";

try {
MessageDigest ex = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(ex.digest(b));
} catch (Exception var3) {
System.out.println("MD5 encode error:" + var3.getMessage());
}

return resultString;
}
}

public static String MoEncode(float f, Timestamp time) {
String ss = String.valueOf(f * (float) time.getMinutes() + (float) time.getSeconds());
String en = MD5Encode(ss);
return en;
}

MD5 加密类使用

标签:valueof   print   return   turn   ESS   string   res   int   stat   

原文地址:https://www.cnblogs.com/fgh2018/p/9408840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!