import java.security.*;
public class MD5 {
????? /**
????? * 字符串加密方法。傳入一個字符串,返回經過<b>MD5</b>加密后的一個字符串
????? *?? @param?? strInput
????? *?? @return
?????? */
????? public??? static?? String encryptMD5(String strInput) {
???????? StringBuffer buf=null;
???????? try {
???????????? MessageDigest md = MessageDigest.getInstance("MD5");
???????????? md.update(strInput.getBytes());
???????????? byte b[] = md.digest();
????????????? buf = new StringBuffer(b.length * 2);
???????????? for(int i=0;i<b.length;i++) {
???????????????? if (((int)b[i] & 0xff) < 0x10) { // & 0xff轉換無符號整型
???????????????????? buf.append("0");
???????????????? }
??????????????? // buf.append(Long.toString((int) b[i] & 0xff, 16));//轉換16進制,下方法同
??????????????? buf.append(Long.toHexString((int)b[i] & 0xff));
???????????? }
???????? }catch(NoSuchAlgorithmException ex) {
????????????? ex.printStackTrace();
???????? }
??????? return buf.toString();
???? }
????? public??? static??? void?? main(String args[]) {
???????? String test?? =??? "abc" ;
???????? System.out.println(encryptMD5(test));
???? }
}
/**
* 下面是一些測試數據:
*?? MD5 ("") = d41d8cd98f00b204e9800998ecf8427e
*
*?? MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661
*
*?? MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72
*
*?? MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0
*
*?? MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b
*/
posted on 2007-05-28 01:24
jadmin 閱讀(46)
評論(0) 編輯 收藏