package com.appdev.bsf.server.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import sun.misc.BASE64Decoder;
/**
* MD5加密
*
* @author wubin
*
*/
public class MD5 {
private static MessageDigest md5;
static {
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String strToBytes(String value) {
if (value == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
try {
byte b[] = decoder.decodeBuffer(value);
return new String(b);
} catch (Exception e) {
return null;
}
}
/* md5算法 */
public static String getMDFivePassword(String password) {
char[] charArray = password.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
public static void main(String[] args) {
String A = null;
try {
System.out.println("请输入你要加密的数据:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
A = br.readLine();
} catch (IOException e) {
}
String postString = MD5.getMDFivePassword(A);
System.out.println("加密后的数据:" + postString);
}
}
MD5加密
最新推荐文章于 2024-08-14 23:12:09 发布