MD5 加密
来源:互联网 发布:阿里算法工程师 编辑:程序博客网 时间:2024/06/10 05:04
package com.main.gm.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private final static String[] hexDigits={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
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(n < 0) {
n=256 + n;
}
int d1=n / 16;
int d2=n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin) {
String resultString=null;
try {
resultString=new String(origin);
MessageDigest md=MessageDigest.getInstance("MD5");
resultString=byteArrayToHexString(md.digest(resultString.getBytes("ISO-8859-1")));
} catch(Exception ex) {
}
return resultString;
}
/**
* MD5 加密
*/
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
public static void main(String[] args) {
String mst ="awardStr%3D3002_50000%3B3001_100%3B3030_5%26body%3D%E9%93%9C%E9%92%B15%E4%B8%87%EF%BC%8C%E4%B8%AD%E7%BB%8F%E9%AA%8C%E4%B9%A6*5%EF%BC%8C%E5%85%83%E5%AE%9D*100%26endDt%3D2015-12-01%2000%3A00%3A01%26isActivity%3D1%26title%3D%E4%B8%83%E6%97%A5%E7%AD%BE%E5%88%B01%26ts%3D1434683735%26type%3D2%26userId%3D110459%26zoneId%3D7&fcf93359372ede6f7c8bbf773f791004";
System.out.println(getMD5Str(mst));
// System.out.println(MD5Encode(mst));
}
}
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private final static String[] hexDigits={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
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(n < 0) {
n=256 + n;
}
int d1=n / 16;
int d2=n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin) {
String resultString=null;
try {
resultString=new String(origin);
MessageDigest md=MessageDigest.getInstance("MD5");
resultString=byteArrayToHexString(md.digest(resultString.getBytes("ISO-8859-1")));
} catch(Exception ex) {
}
return resultString;
}
/**
* MD5 加密
*/
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
public static void main(String[] args) {
String mst ="awardStr%3D3002_50000%3B3001_100%3B3030_5%26body%3D%E9%93%9C%E9%92%B15%E4%B8%87%EF%BC%8C%E4%B8%AD%E7%BB%8F%E9%AA%8C%E4%B9%A6*5%EF%BC%8C%E5%85%83%E5%AE%9D*100%26endDt%3D2015-12-01%2000%3A00%3A01%26isActivity%3D1%26title%3D%E4%B8%83%E6%97%A5%E7%AD%BE%E5%88%B01%26ts%3D1434683735%26type%3D2%26userId%3D110459%26zoneId%3D7&fcf93359372ede6f7c8bbf773f791004";
System.out.println(getMD5Str(mst));
// System.out.println(MD5Encode(mst));
}
}
0 0
- MD5加密
- MD5加密
- md5加密
- MD5加密
- md5加密
- MD5加密
- MD5加密
- md5 加密
- MD5加密
- md5加密
- MD5加密
- MD5加密
- MD5加密
- MD5加密
- MD5加密
- MD5加密
- MD5加密
- MD5加密
- 中断线 申请 外设——PCI中断路由机制
- java多线程-Lock接口(将多线程共享资源锁定)
- 数据中心(IDC)的灾难恢复目标以及虚拟机迁移帮助灾难恢复
- 红帽发布首个RHEL 7测试版本
- Canvas学习笔记
- MD5 加密
- AMD:引入ARM将是自64位以来的最大变革
- Google带WiFi的热气球或首次商用
- (OK) Windows XP 硬盘安装 RHEL7/CentOS7/Fedora19/Fedora20
- PostgreSQL 回归测试
- Windows 7 硬盘安装 RHEL7/CentOS7/Fedora19/Fedora20
- 阿里云上搭建网站
- QrCode二维码
- Xcode之修改系统生成的注释模板