package cn.ty;
import java.security.*;
import java.util.*;
public class Md5 {
private String inStr;
private MessageDigest md5;
/**
* Constructs the MD5 object and sets the string whose MD5 is to be computed.
*
* @param inStr the String
whose MD5 is to be computed
*/
public Md5(String inStr)
{
this.inStr = inStr;
try
{
this.md5 = MessageDigest.getInstance("MD5");
} catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
}
}
/**
* Computes the MD5 fingerprint of a string.
*
* @return the MD5 digest of the input String
*/
public String compute()
{
char[] charArray = this.inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i=0; i byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = this.md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i=0; 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)
{
Md5 md5=new Md5("admin");
String postString = md5.compute();
System.out.println(postString);
}
}
测试效果与PHP里面的md5函数效果一样!
结论:以后使用此类进行JAVA中的MD5加密输出!
阅读(687) | 评论(1) | 转发(0) |