熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

一個實現MD5的簡潔的JAVA類

2022-06-13   來源: Javascript 

  由於消息摘要唯一性和不可逆性的特點所以不失為一種簡單的常用的加密手段比如你可以用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
From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25434.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.