由于消息摘要唯一性和不可逆性的特点所以不失为一种简单的常用的加密手段比如你可以用md来加密你的应用中的用户口令
package test;
import javasecurityMessageDigest;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) </p>
* <p>Company: </p>
* @author unascribed
* @version
*/
public class StringUtil {
private final static String[] hexDigits = {
a b c d e f};
/**
* 转换字节数组为进制字串
* @param b 字节数组
* @return 进制字串
*/
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = ; i < blength; i++) {
resultSbappend(byteToHexString(b[i]));
}
return resultSbtoString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < )
n = + n;
int d = n / ;
int d = n % ;
return hexDigits[d] + hexDigits[d];
}
public static String MDEncode(String origin) {
String resultString = null;
try {
resultString=new String(origin);
MessageDigest md = MessageDigestgetInstance(MD);
resultString=byteArrayToHexString(mddigest(resultStringgetBytes()));
}
catch (Exception ex) {
}
return resultString;
}
public static void main(String[] args){
Systemerrprintln(MDEncode(a));
}
}
在RFC 中给出了Test suite用来检验你的实现是否正确
MD () = ddcdfbeecfe
MD (a) = ccbcfbace
MD (abc) = cdfbdfdef
MD (message digest) = fbdcbdafaafd
MD (abcdefghijklmnopqrstuvwxyz) = cfcddedfbccaeb