java md5 加密

/**
  * md5或者sha-1加密
  *
  * @param inputText
  *            要加密的內容
  * @param algorithmName
  *            加密算法名稱:md5或者sha-1,不區分大小寫
  * @return
  */
 private static String encrypt(String inputText, String algorithmName) {
  if (inputText == null || "".equals(inputText.trim())) {
   throw new IllegalArgumentException("請輸入要加密的內容");
  }
  if (algorithmName == null || "".equals(algorithmName.trim())) {
   algorithmName = "md5";
  }
  String encryptText = null;
  try {
   MessageDigest m = MessageDigest.getInstance(algorithmName);
   m.update(inputText.getBytes("UTF8"));
   byte s[] = m.digest();
   // m.digest(inputText.getBytes("UTF8"));
   return hex(s);
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return encryptText;
 }
發佈了34 篇原創文章 · 獲贊 20 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章